How to detect which button and at which position clicked in CustomList

岁酱吖の 提交于 2019-12-12 16:29:50

问题


I am using a custom listview in which i have two button for every item. following is the my item 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>

and Following is the Addapter I am using.

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;
    }

}

and I am setting list view in following code.

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;
            }           
        }
    };

}

Problem: Custom List View is Not being registred with setOnItemClickListener(mListner); Actualy I want to detect which button and at which position has been clicked in SectionFragment class.


回答1:


This will work

    @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;
}
}  

and in 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:


I added onClick in the getView which works for me. Code:

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;
    }


来源:https://stackoverflow.com/questions/16162189/how-to-detect-which-button-and-at-which-position-clicked-in-customlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!