1

大家好,我有自定义网格视图与 imageButton 和 textView 如下

在此处输入图像描述

在这里我使用了 imageButton.. 问题是网格不可点击.. 但是如果我在这里使用 imageView 它工作正常但 UI 不合适。

这是我的布局

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:gravity="center"
    android:padding="5dp"
    >

    <ImageButton
        android:id="@+id/profilePic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/newlocation" 
        android:background="@drawable/btnbackground"
        />

    <TextView
        android:id="@+id/SpeakerName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="15sp"
        android:text="Some Speaker"
        />

这是我的适配器

    package com.tt.cc;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SavedConferencesGridAdapter extends ArrayAdapter<String>{


    Context mContext;
    List<String> confernceList;

    public SavedConferencesGridAdapter(Context context, int textViewResourceId, List<String> confernceList) 
    {

        super(context,textViewResourceId,confernceList);
        this.confernceList =  confernceList;
        this.mContext = context;
        Log.e("TAG", confernceList.size()+"");
    }

    int[] picIds = new int[] { R.drawable.newschedule,R.drawable.newexhibitors,
                                R.drawable.newsponsers, R.drawable.newlocation,
                                R.drawable.newfavourites, R.drawable.newreminder,R.drawable.info,R.drawable.info,R.drawable.info,R.drawable.info,R.drawable.info,R.drawable.info };


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return confernceList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v;



        if(convertView==null){
            LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = li.inflate(R.layout.entity, null);
            TextView tv = (TextView)v.findViewById(R.id.SpeakerName);
            tv.setText("tt");
            /*ImageView iv = (ImageView) v.findViewById(R.id.profilePic);
            iv.setImageResource(picIds[position]);*/

        }
        else
        {
            v = convertView;
        }
        return v;
    }



}

这是我的活动

 public class SavedConferencesActivity extends Activity{

    GridView confereneceGridView;
    ArrayAdapter<String> conferneceAdapter;
    Button btnUpdate;

    List<String> conferenceList;
    TextView txtHeading;
    Boolean result = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


    }


    protected void onStart() {
        super.onStart();
        setContentView(R.layout.homegridlayout);
        initialize();

        confereneceGridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Toast.makeText(SavedConferencesActivity.this, conferenceList.get(position)+"", Toast.LENGTH_SHORT).show();


    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        conferneceAdapter = new SavedConferencesGridAdapter(this, android.R.layout.simple_list_item_1, conferenceList);
        confereneceGridView.setAdapter(conferneceAdapter);
    }


    private void initialize() {


        confereneceGridView = (GridView) findViewById(R.id.gridOfConference);
        btnUpdate = (Button) findViewById(R.id.btnUpdate);
        txtHeading = (TextView) findViewById(R.id.txtTag);

        conferenceList = new ArrayList<String>();
        conferenceList.add("AA");
        conferenceList.add("BB");
        conferenceList.add("CC");

        if (conferenceList.size() == 0)
            txtHeading.setText("No conferences saved");
        else
            txtHeading.setText("Selected Conferences");



    }




}

请帮我解决这个问题。或者,如果有任何替代方案可用..

4

4 回答 4

1

简单的。为 ImageButton 提供此属性,

android:focusable="false"

当 Grid 中有可聚焦的元素时,通常会出现此问题。(例如:按钮、图像按钮等)。

于 2012-05-02T07:46:29.593 回答
1

这个 ImageButton 的原因是默认情况下是可点击和可聚焦的,所以 Focus 和 click 事件永远不会转到 ItemView。

设置属性

android:focusable-"false" 

android:clickable="false"
于 2012-05-02T07:48:18.923 回答
1

您的项目的布局应该是:

<LinearLayout ...
    android:descendantFocusability="blocksDescendants">
<ImageButton ...
    android:focusable="false"
    android:focusableInTouchMode="false"/>
</LinearLayout>

不要忘记适配器中 ImageButton 的 setOnClickListener。

于 2015-07-22T08:43:25.330 回答
0

在您的适配器类中,在返回视图“v”之前,将“ onClickListener”添加到“v”

 v.setOnClickListener(new OnClickListener() {           
        @Override
        public void onClick(View v) {
                Toast.makeText(context, position, Toast.LENGTH_SHORT).show();
        }
 });
于 2012-05-02T08:05:39.927 回答