1 .Model.java
public class Model{
private int icon;
private String title;
private boolean isGroupHeader = false;
public Model(String title) {
setGroupHeader(true);
}
public Model(int icon, String title) {
super();
this.setIcon(icon);
this.setTitle(title);
}
public boolean isGroupHeader() {
return isGroupHeader;
}
public void setGroupHeader(boolean isGroupHeader) {
this.isGroupHeader = isGroupHeader;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
//gettters & setters...
}
这是我的MyAdapter.java代码
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends ArrayAdapter<Model> {
private final Context context;
private final ArrayList<Model> modelsArrayList;
public MyAdapter(Context context, ArrayList<Model> modelsArrayList) {
super(context, R.layout.item1, modelsArrayList);
this.context = context;
this.modelsArrayList = modelsArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = null;
if(!modelsArrayList.get(position).isGroupHeader()){
rowView = inflater.inflate(R.layout.item1, parent, false);
// 3. Get icon,title & counter views from the rowView
ImageView imgView = (ImageView) rowView.findViewById(R.id.item_icon);
TextView titleView = (TextView) rowView.findViewById(R.id.item_title);
// 4. Set the text for textView
imgView.setImageResource(modelsArrayList.get(position).getIcon());
titleView.setText(modelsArrayList.get(position).getTitle());
}
else{
rowView = inflater.inflate(R.layout.group_header_item, parent, false);
TextView titleView = (TextView) rowView.findViewById(R.id.header);
titleView.setText(modelsArrayList.get(position).getTitle());
}
// 5. retrn rowView
return rowView;
}}
这是我的MainActivity代码
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// if extending Activity
//setContentView(R.layout.activity_main);
// 1. pass context and data to the custom adapter
MyAdapter adapter = new MyAdapter(this, generateData());
// if extending Activity 2. Get ListView from activity_main.xml
//ListView listView = (ListView) findViewById(R.id.listview);
// 3. setListAdapter
//listView.setAdapter(adapter); if extending Activity
setListAdapter(adapter);
}
private ArrayList<Model> generateData(){
ArrayList<Model> models = new ArrayList<Model>();
models.add(new Model("Group Title 1"));
models.add(new Model(R.drawable.ic_launcher,"Menu Item"));
models.add(new Model(R.drawable.app_icon,"Menu Item"));
models.add(new Model(R.drawable.ic_launcher,"Menu Item 3"));
models.add(new Model("Group Title 2"));
models.add(new Model(R.drawable.ic_launcher,"Menu Item 1"));
models.add(new Model(R.drawable.app_icon,"Menu Item 2"));
models.add(new Model(R.drawable.ic_launcher,"Menu Item 3"));
models.add(new Model("Group Title 3"));
models.add(new Model(R.drawable.ic_launcher,"Menu Item 1"));
models.add(new Model(R.drawable.app_icon,"Menu Item 2"));
models.add(new Model(R.drawable.ic_launcher,"Menu Item 3"));
return models;
}}
我在项目中创建了一个自定义列表视图。 如何使用图像和文本setonclick listener to whole raw。
我在为项目的不同部分创建单独的onClickListener时遇到问题。我的项目有图片和文字。我想要的是根据点击的那些开始不同的活动。
我该怎么做?
答案 0 :(得分:0)
制作适配器工具View.OnClickListener
并将其设置为TextView
和ImageView
,然后在实施的onClick
中检查视图实例并执行不同的操作。
如果您想要更复杂的事情发生,就像不同行中的不同视图一样,将position
tag
设置为您的视图,并在onClick
当我说设置位置作为标记我的意思是imgView.setTag(position)
&amp; titleView.setTag(position)
通过这种方式,您可以了解onClick
中的位置,并成为我的客人,尝试使用大型样本集。这是一个jank / lag免费实现。通常作为经验法则,您必须避免在Adapter's
getView()
中实例化对象,因为GC可能会使主线程无响应。
答案 1 :(得分:0)
在适配器的getView()方法中为ImageView和textView实现clickListener。
答案 2 :(得分:0)
您必须执行以下操作:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
// I edited here
final Model _model = modelsArrayList.get(position);
View rowView = null;
if(!_model.isGroupHeader()){
rowView = inflater.inflate(R.layout.item1, parent, false);
// 3. Get icon,title & counter views from the rowView
ImageView imgView = (ImageView) rowView.findViewById(R.id.item_icon);
TextView titleView = (TextView) rowView.findViewById(R.id.item_title);
// 4. Set the text for textView
imgView.setImageResource(modelsArrayList.get(position).getIcon());
titleView.setText(modelsArrayList.get(position).getTitle());
// Set the listeners
imgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(_model is what you want){
//call your activity
}
}
});
titleView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(_model is what you want){
//call your activity
}
}
});
}
else{
rowView = inflater.inflate(R.layout.group_header_item, parent, false);
TextView titleView = (TextView) rowView.findViewById(R.id.header);
titleView.setText(modelsArrayList.get(position).getTitle());
}
// 5. retrn rowView
return rowView;
}
不要忘记在{x}
中的android:clickable="true"
添加ImageView
答案 3 :(得分:0)
多数民众赞成。
只需创建ListView的XML文件(此处为main.xml)
并设置ListView(这里是lv1)
请记住:如果您extends Activity
,请不要忘记setContentView
如果您想extends ListActivity
不要使用setcontentView
public class starting extends Activity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// if extending Activity
setContentView(R.layout.main);
// 1. pass context and data to the custom adapter
MyAdapter adapter = new MyAdapter(this, generateData());
// if extending Activity 2. Get ListView from activity_main.xml
ListView listView = (ListView) findViewById(R.id.lv1);
// 3. setListAdapter
listView.setAdapter(adapter);// if extending Activity
//setListAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switch ( arg2 )
{
case 0 : Toast.makeText(getApplicationContext(), "fst opt", Toast.LENGTH_SHORT).show();
break;
case 1 : Toast.makeText(getApplicationContext(), "sec opt", Toast.LENGTH_SHORT).show();
break;
case 2 : Toast.makeText(getApplicationContext(), "thrd opt", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
private ArrayList<Model> generateData(){
ArrayList<Model> models = new ArrayList<Model>();
models.add(new Model("Group Title 1"));
models.add(new Model(R.drawable.ic_launcher,"Menu Item"));
models.add(new Model(R.drawable.app_icon,"Menu Item"));
models.add(new Model(R.drawable.ic_launcher,"Menu Item 3"));
models.add(new Model("Group Title 2"));
models.add(new Model(R.drawable.ic_launcher,"Menu Item 1"));
models.add(new Model(R.drawable.app_icon,"Menu Item 2"));
models.add(new Model(R.drawable.ic_launcher,"Menu Item 3"));
models.add(new Model("Group Title 3"));
models.add(new Model(R.drawable.ic_launcher,"Menu Item 1"));
models.add(new Model(R.drawable.app_icon,"Menu Item 2"));
models.add(new Model(R.drawable.ic_launcher,"Menu Item 3"));
return models;
}
}
答案 4 :(得分:-1)
您可以为行视图设置onclick侦听器。
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});