【问题标题】:How to detect which button and at which position clicked in CustomList如何检测在CustomList中单击了哪个按钮和哪个位置
【发布时间】:2013-04-23 06:10:26
【问题描述】:

我正在使用自定义列表视图,其中每个项目都有两个按钮。以下是我的项目 xml。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:weightSum="10">

            <Button
                android:id="@+id/btn_sound"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_gravity="left"
                android:layout_marginLeft="3dp"
                android:layout_marginTop="3dp"
                android:layout_marginBottom="3dp"
                android:layout_marginRight="0dp"
                android:background="@drawable/btn_background"
                android:gravity="left|center_vertical"
                android:paddingLeft="10dp"
                android:text="Sound Name"
                android:textSize="20sp"
                android:textStyle="bold"
                 android:layout_weight="7" />

            <ImageButton
                android:id="@+id/ib_info"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_gravity="right"
                android:gravity="left|center_vertical"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="3dp"
                android:layout_marginBottom="3dp"
                android:layout_marginRight="3dp"
                android:layout_weight="3"
                android:src="@drawable/btn_info_source"
                android:background="@drawable/btn_info_background" />

</LinearLayout>

Following 是我正在使用的 Addapter。

public class CustomListAdapter extends BaseAdapter implements OnClickListener {
    Context mContext;
    String[] mArrayList;

    public CustomListAdapter(Context context, String[] SoundNames) {
        this.mContext = context;
        this.mArrayList = SoundNames;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            LayoutInflater inflater1 = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater1.inflate(R.layout.list_item, null);
            holder.btn_sound = (Button) convertView.findViewById(R.id.btn_sound);
            holder.ib_info = (ImageButton) convertView.findViewById(R.id.ib_info);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        holder.btn_sound.setText(mArrayList[position]);
        //holder.btn_sound.setOnClickListener(this);
        return convertView;
    }

    private static class Holder {

        Button btn_sound;
        ImageButton ib_info;
    }

    @Override
    public int getCount() {
        return mArrayList.length;
    }

    @Override
    public Object getItem(int position) {
        return mArrayList[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

}

我正在以下代码中设置列表视图。

public class SectionFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView;
        ListView mListView;
        Bundle args = getArguments();
        int currentView = args.getInt(Constants.ARG_SECTION_NUMBER) - 2;

        if (currentView == 0) {
            rootView = inflater.inflate(R.layout.deer, container, false);
            //mListView = (ListView) rootView.findViewById(R.id.listView_deer);
            CustomListAdapter mListAdapter = new CustomListAdapter(MainActivity.appContext, Constants.deer_Sound_Names);
            //mListView.setAdapter(mListAdapter);
            ((AdapterView<ListAdapter>) rootView.findViewById(R.id.listView_deer)).setAdapter(mListAdapter);
            ((AdapterView<ListAdapter>) rootView.findViewById(R.id.listView_deer)).setOnItemClickListener(mListner);


        } else if (currentView == 1) {
            rootView = inflater.inflate(R.layout.guide_layout, container, false);
            Toast.makeText(MainActivity.appContext, "Loading Guide...", Toast.LENGTH_LONG).show();
            WebView guideViewer = (WebView) rootView.findViewById(R.id.webView_guide);
            guideViewer.loadUrl("file:///android_asset/deer_html/guide.htm");

        } else {

            rootView = inflater.inflate(R.layout.activity_main, container,
                    false);
        }
        return rootView;
    }
    OnItemClickListener mListner = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
            Log.d("HFI","Item Clicked: "+Constants.deer_Sound_Index[position]);
            switch (view.getId()) {
            case R.id.btn_sound:
                Log.d("HFI","Sound: "+Constants.deer_Sound_Index[position]);
                break;
            case R.id.ib_info:
                Log.d("HFI","Info: "+Constants.deer_Sound_Index[position]);
                break;
            default:
                break;
            }           
        }
    };

}

问题:自定义列表视图未注册到 setOnItemClickListener(mListner); 实际上,我想检测在SectionFragment 类中单击了哪个按钮和哪个位置。

【问题讨论】:

  • position.. 在 mListner 中是给你哪个位置被点击了..
  • 为什么不在holder.btn上设置onClockListener呢?这就是你所要做的。
  • @DhawalSodha mListner 从未打过电话。这就是我在这里提出这个问题的原因。
  • @Ascorbin 如你所见holder.btn_sound.setOnClickListener(this); 我已经这样做了。但这样我就没有得到按钮的位置。只获取与我的所有列表项相同的按钮。
  • @AhmedNawaz:您在 SectionFragment 中声明了 OnItemClickListener 并且适配器在 CustomListAdapter 中。在 CustomListAdapter 类中添加 OnItemClickListener 检查这个,它会给你想法 --> stackoverflow.com/a/12813906/1168654

标签: android android-listview custom-lists


【解决方案1】:

这会起作用

    @SuppressWarnings("unchecked")
public class ViewHolder
{

private static final String TAG = "View Holder";

public static <T extends View> T get(View view, int id) 
{
    SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
    if (viewHolder == null) 
    {
        viewHolder = new SparseArray<View>();
        view.setTag(viewHolder);
    }
    View childView = viewHolder.get(id);
    if (childView == null) 
    {
        childView = view.findViewById(id);
        viewHolder.put(id, childView);
    }
    return (T) childView;
}

public static <T extends View> T get(View view, int id, int position) 
{
    SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
    if (viewHolder == null) 
    {
        viewHolder = new SparseArray<View>();
        view.setTag(viewHolder);
    }
    View childView = viewHolder.get(id);

    if (childView == null) 
    {
        childView = view.findViewById(id);
        viewHolder.put(id, childView);
    }
    childView.setTag(position);
    return (T) childView;
}
}  

在getView中

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {

        LayoutInflater inflater1 = (LayoutInflater) this.mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater1.inflate(R.layout.list_item, null);

    } 
    Button btn_sound = ViewHolder.get(findViewById(R.id.btn_sound), position);
        ImageButton ib_info = ViewHolder.get(findViewById(R.id.ib_info));


    btn_sound.setText(mArrayList[position]);
    btn_sound.setOnClickListener(this);
    return convertView;
}  

 @Override
    public void onClick(View v) { 
        int position = (Integer)v.getTag();
        // do whatever you want
    }

【讨论】:

  • 我复制粘贴了,错过了最重要的东西。我编辑了我的答案。
【解决方案2】:

我在getView 中添加了对我有用的 onClick。 代码:

public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            LayoutInflater inflater1 = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater1.inflate(R.layout.list_item, null);
            holder.btn_sound = (Button) convertView.findViewById(R.id.btn_sound);
            holder.ib_info = (ImageButton) convertView.findViewById(R.id.ib_info);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        holder.btn_sound.setText(mArrayList[position]);

        holder.btn_sound.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //Sound Button Click
            }
        });
        holder.ib_info.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //info Button Click
            }
        });
        return convertView;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 2017-03-28
    • 2014-10-13
    • 1970-01-01
    相关资源
    最近更新 更多