我实施了一个自定义适配器,用一个ListView
和2个image
填充自定义TextField
。现在我想要做的是添加动态向下滚动列表的新行。我可以做到这个结果吗?
这是适配器的代码
public class CustomListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
private final String[] questions;
public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid, String[] questions) {
super(context, R.layout.mylist, itemname);
// TODO Auto-generated constructor stub
this.context = context;
this.itemname = itemname;
this.imgid = imgid;
this.questions=questions;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.mylist, null, true);
rowView.setPadding(0,10,0,10);
TextView txtTitle = (TextView) rowView.findViewById(R.id.nameList);
txtTitle.setTextColor(Color.BLACK);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imgList);
TextView extratxt = (TextView) rowView.findViewById(R.id.question);
txtTitle.setText(itemname[position]);
imageView.setImageDrawable(new RoundImage(BitmapFactory.decodeResource(context.getResources(), imgid[position])));
//imageView.setImageResource(imgid[position]);
extratxt.setText(questions[position]);
return rowView;
};
}
这是列表所在的fragment
代码的一部分
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {
this.adapter=new CustomListAdapter(getActivity(), itemname, imgid, questions);
list=(ListView)firstAccessView.findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//stuff
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem= itemname[+position];
Toast.makeText(getActivity().getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
}
});
}
答案 0 :(得分:1)
覆盖onScroll()
片段中的onCreateView()
方法,实现onScrollListener
。
将您的问题数组字符串更改为ArrayList。你也可以为其他名单做这件事。
private ArrayList<String> questions;
public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid, ArrayList<String> questions) {
super(context, R.layout.mylist, itemname);
// TODO Auto-generated constructor stub
this.context = context;
this.itemname = itemname;
this.imgid = imgid;
this.questions=questions;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.mylist, null, true);
rowView.setPadding(0,10,0,10);
TextView txtTitle = (TextView) rowView.findViewById(R.id.nameList);
txtTitle.setTextColor(Color.BLACK);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imgList);
TextView extratxt = (TextView) rowView.findViewById(R.id.question);
txtTitle.setText(itemname[position]);
imageView.setImageDrawable(new RoundImage(BitmapFactory.decodeResource(context.getResources(), imgid[position])));
//imageView.setImageResource(imgid[position]);
extratxt.setText(questions.get(position));
return rowView;
};
}
实施onScroll
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//load your new set of data here.
your_adapter.notifyDataSetChanged();
}
答案 1 :(得分:-1)
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//loadMoreData
//if using arrayAdapter, use this
for (String i: itemname) {
adapter.add(i);
}
adapter.notifyDataSetChanged();
}