【问题标题】:Nothings happening on clicking items of GridView单击 GridView 的项目时没有发生任何事情
【发布时间】:2013-11-20 12:54:51
【问题描述】:

我正在使用 GridView 来显示类别,我在网格视图中使用了 ImageButton 和文本视图,但我无法在这些项目上设置 OnItemClickListener(我的意思是点击它时没有发生任何事情) 我的代码是 这是我的 GridView 适配器

public class MyAdapter extends BaseAdapter{
    private ArrayList<String> listJewel_name;
    private ArrayList<Integer> listJewellery;
    private Activity activity;
    //private LayoutInflater inflater;
    public MyAdapter(Activity activity,ArrayList<String> listJewel_name, ArrayList<Integer> listJewellery) {
        super();
        this.listJewel_name = listJewel_name;
        this.listJewellery = listJewellery;
        this.activity = activity;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listJewel_name.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listJewel_name.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder
    {
        public ImageButton imgViewJewel;
        public TextView txtViewTitle;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if(convertView==null)
        {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.gridview, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.tvtext);
            view.imgViewJewel = (ImageButton) convertView.findViewById(R.id.picture);

            convertView.setTag(view);
        }
        else
        {
            view = (ViewHolder) convertView.getTag();
        }

        view.txtViewTitle.setText(listJewel_name.get(position));
        view.imgViewJewel.setImageResource(listJewellery.get(position));

        return convertView;
    }
} 

我的activity.class是

GridView gridView;
    Button add;
    private MyAdapter mAdapter;
    private ArrayList<String> listJewel_name;
    private ArrayList<Integer> listJewellery;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_jewellery_option);
        prepareList();
         // prepared arraylist and passed it to the Adapter class
        mAdapter = new MyAdapter(this,listJewel_name, listJewellery);

        // Set custom adapter to gridview
        gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(mAdapter);

        // Implement On Item click listener
       gridView.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position,
                    long id) {
                String shri=mAdapter.getItem(position);
                Toast.makeText(SelectJewelleryOption.this, shri , Toast.LENGTH_SHORT).show();
            }
        });
    }

我的 XML 文件是

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<com.myjewelbox.SquareImageButtonView
    android:id="@+id/picture"
    android:layout_width="match_parent"
    android:layout_height="130dp"
    android:scaleType="fitCenter"
    android:clickable="true" />

    <TextView
        android:id="@+id/tvtext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="1dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_gravity="bottom"
        android:textColor="@color/Golden"
        android:background="#55000000"
        android:focusable="false"
        />
</FrameLayout>

我已将此 XMl 文件设置为我的主 XML 文件的 GridView

【问题讨论】:

  • 你能分享你的gridview布局文件吗?
  • @Homosapiens 我已经添加了 CML 文件,请参阅我还添加了我的 XML 的外观
  • 从子视图中删除 android:clickableandroid:focusable 并仅在 gridview 上设置。

标签: java android gridview


【解决方案1】:

为GridView的适配器设置ItemClickListener,而不是直接为GridView设置ItemClickListener。

view.imgViewJewel.setItemClickListener(activity);

【讨论】:

  • 请检查更新并建议我请@azerto00
  • 在适配器的 getview() 中的视图上设置 setItemClickListener(),例如 ---- view.txtViewTitle.setText(listJewel_name.get(position)); view.imgViewJewel.setImageResource(listJewellery.get(position)); view.imgViewJewel.setOnClickListener(listner) return convertView;
  • setOnClickListener 代替 setItemClickListener() **
【解决方案2】:

请把你的xml改成这个-

添加thease 2 line-

android:clickable="true"

android:focusable="false"

 <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <com.myjewelbox.SquareImageButtonView
            android:id="@+id/picture"
            android:layout_width="match_parent"
            android:layout_height="130dp"
            android:clickable="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:scaleType="fitCenter" />

        <TextView
            android:id="@+id/tvtext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="#55000000"
            android:focusable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="1dp"
            android:paddingTop="10dp"
            android:textColor="@color/Golden" />

    </FrameLayout>

更多详情请看这里- http://www.androidhub4you.com/2013/07/custom-grid-view-example-in-android.html

【讨论】:

  • 那么你做错了什么。只需从此处下载代码并替换您的图像和文本-github.com/manishsri01/CustomGridView
  • 它应该可以工作。做一件事,创建你的演示项目并通过 manishupacc@gmail.com 给我发电子邮件,但演示应该处于工作状态..
  • 其实这东西是我项目的一部分我会把整个项目邮寄给你好吗?
  • 没有,只有相关文件放在那里,而不是整个项目。否则需要更多时间来理解我。我会在晚上检查并回复你。
  • 如果我不发送整个文件,它就不会工作,这就是为什么...请查看我寄给你的邮件
【解决方案3】:

在您的适配器类中,在返回线之前在您的 convertView 上设置 OnClickListener。

convertView.setOnClickListener(new onClickListener(){
    //In this method you can get the items same as you are getting in your
    //adapter, as follow:-

    String name=listJewel_name.get(position);

    //or you can do whatever you want as now you have the whole view that is 
    //clicked, so you can event get its children or start a new activity or 
    //whatever you want
}
return converView;

【讨论】:

    【解决方案4】:

    确保android:focusable="false" 在您的First ChildGridView Layout

    GridViews 无法处理可点击项目。 Button 例如不起作用。此外,如果您有任何其他 non-clickable 项目,您必须确保您没有设置属性:clikable="true"。

    请参考以下链接 - 与您的预期相同

    http://www.androidhub4you.com/2013/07/custom-grid-view-example-in-android.html

    【讨论】:

    • 我设置了那些 clicable 和 focusable 的属性,但什么也没发生
    • 你能分享那个xml文件吗?
    • 您能否将 android:clickable="false" 设置为您的 SquareImageButtonView 并检查一下?
    • 现在它甚至没有检测到点击,之前它正在检测点击,但没有发生任何事情
    • “以前它检测到点击但什么也没发生”是什么意思?
    猜你喜欢
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 2021-05-13
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多