我正在使用自定义列表视图,其中每个项目都有两个按钮。以下是我的项目 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。
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
课堂上单击了哪个按钮和哪个位置。