【发布时间】:2015-09-01 04:25:47
【问题描述】:
我正在尝试使用自定义适配器和片段制作自定义列表。我可以制作列表视图。但是有一个问题,我连续有一个图像按钮和一个文本视图。我想监听该行的点击事件,以及图片按钮的点击事件。换句话说,我需要添加一个侦听器,该侦听器将在单击该行以及单击该行上存在的图像按钮时进行侦听。
这是我的代码:
自定义适配器
public class CustomAdapter extends BaseAdapter implements View.OnClickListener {
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater = null;
public CustomAdapter(Activity a, ArrayList d) {
/********** Take passed values **********/
activity = a;
data = d;
inflater = (LayoutInflater) activity.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
if (data.size() <= 0)
return 1;
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public void onClick(View v) {
Log.d("row is click","row click");
}
/*********
* Create a holder Class to contain inflated xml file elements
*********/
public static class ViewHolder {
public TextView text;
public ImageButton imageButton;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.row_layout, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.text = (TextView) vi.findViewById(R.id.station_name);
holder.imageButton = (ImageButton) vi.findViewById(R.id.favorite);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
if (data.size() <= 0) {
holder.text.setText("No Data");
} else {
String string = (String) data.get(position);
/************ Set Model values in Holder elements ***********/
holder.text.setText(string);
}
return vi;
}
}
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/station_name"
android:padding="10dp"
android:textColor="#eee345"
android:textAppearance="?android:textAppearanceLarge"
/>
<ImageButton android:id="@+id/favorite"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/start"
android:background="#00ffffff"
/>
</LinearLayout>
fragmentone.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#325633"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_view">
</ListView>
</LinearLayout>
fragmentone.java
public class Fragmentone extends Fragment{
ArrayList<String> name;
boolean isPressed=false;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
name=new ArrayList<String>();
name.add("First Station");
name.add("Second Station");
ListView listView = (ListView) view.findViewById(R.id.list_view);
CustomAdapter customAdapter =new CustomAdapter(getActivity(),name);
listView.setAdapter(customAdapter);
return view;
}
}
【问题讨论】:
-
非常简单,为您的父布局添加点击监听器,这样您就可以监听您的跨行点击并为您的行中的单个视图添加点击监听器,从您的列表视图中删除 itemclick 监听器
-
你能不能给一些代码
-
只需添加convertView.setOnClickListener
标签: android android-layout android-fragments android-intent