本文介绍了如何检测哪个按钮,并在该位置CustomList点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用中,我对每一个项目两个Button的自定义列表视图。以下是我的项目的XML。
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>
和以下是我使用的Addapter。
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;
}
}
和我设置列表视图在以下code。
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;
}
}
};
}
问题:自定义列表视图不被registred与 setOnItemClickListener(mListner);
Actualy我想检测哪个按钮,并在该位置已经被点击了 SectionFragment
类。
推荐答案
我在 getView
这对我的作品添加的onClick。
code:
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;
}
这篇关于如何检测哪个按钮,并在该位置CustomList点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!