使用自定义视图填充 ListFragments?
以前,我可以将布局扩展为 ListView 的自定义视图层次结构。但我不知道如何对 listFragment 执行相同的操作。假设我有一个 item_list 布局,其中有一个 ImageView 和 2 个文本视图。我想将其膨胀以在我的 ListFragment 中使用。但如何?
public class Tab1Fragment extends ListFragment {
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
static final String[] title = new String[] {
"Classic Plank", "Side Plank", "Reversed Plank", "Swissball Plank", "One Arm One Leg", //5
};
static final String[] description = new String[] {
"Variation of Sushis from fresh vegetables and seafoods! Good for family occassions!",
"Oyakodon is a Japanese Rice Bowl dish with Chicken, Eggs and various sorts of healthy and delicious Veggetables!",
"Japanese assorted Pancake that is made from many different ingredients. The taste is so delicious!",
"Japanese Dumplings made of Rice Flour. This is one of the healthiest sweets in Japan!",
"Japanese assorted stews. Made from many different kind of veggetables. Popular in Winter!", //5
};
private Integer[] imgid = {
R.drawable.thumb_classic, R.drawable.thumb_side, R.drawable.thumb_reverse, R.drawable.thumb_swissball, R.drawable.thumb_reachout, //5
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easylist);
mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for(int i=0;i<title.length;i++){
try {
rd = new RowData(i,title[i],description[i]);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.list_item_main, android.R.id.list, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
private void setContentView(int main) {
// TODO Auto-generated method stub
}
private LayoutInflater getSystemService(String layoutInflaterService) {
// TODO Auto-generated method stub
return null;
}
public void onListItemClick(ListView listView, View view, int position, long id) {
Bundle bundle = new Bundle();
bundle.putInt("Layout", position);
Intent newIntent = new Intent(this.getApplicationContext(), ContentViewer.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
}
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
private class RowData {
protected int mId;
protected String mTitle;
protected String mDescription;
RowData(int id, String title, String description){
mId=id;
mTitle = title;
mDescription = description;
}
@Override
public String toString() {
return mId+" "+mTitle+" "+mDescription;
}
}
private class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource, int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
TextView description = null;
ImageView thumbnail = null;
RowData rowData = getItem(position);
if(null == convertView){
convertView = mInflater.inflate(R.layout.list_item_main, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.gettitle();
title.setText(rowData.mTitle);
description = holder.getdescription();
description.setText(rowData.mDescription);
thumbnail = holder.getImage();
thumbnail.setImageResource(imgid[rowData.mId]);
return convertView;
}
private class ViewHolder {
private View mRow;
private TextView title = null;
private TextView description = null;
private ImageView thumbnail = null;
public ViewHolder(View row) {
mRow = row;
}
public TextView gettitle() {
if(null == title){
title = (TextView) mRow.findViewById(R.id.title);
}
return title;
}
public TextView getdescription() {
if(null == description){
description = (TextView) mRow.findViewById(R.id.description);
}
return description;
}
public ImageView getImage() {
if(null == thumbnail){
thumbnail = (ImageView) mRow.findViewById(R.id.thumbnail);
}
return thumbnail;
}
}
}
}
previously, I had no problem with inflating a layout as a custom view hierarchy for a ListView. But I have no clue on how to do the same for a listFragment. Lets say I have an item_list layout with an ImageView and 2 textviews in it. I want to inflate this to be used in my ListFragment. But how??
public class Tab1Fragment extends ListFragment {
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
static final String[] title = new String[] {
"Classic Plank", "Side Plank", "Reversed Plank", "Swissball Plank", "One Arm One Leg", //5
};
static final String[] description = new String[] {
"Variation of Sushis from fresh vegetables and seafoods! Good for family occassions!",
"Oyakodon is a Japanese Rice Bowl dish with Chicken, Eggs and various sorts of healthy and delicious Veggetables!",
"Japanese assorted Pancake that is made from many different ingredients. The taste is so delicious!",
"Japanese Dumplings made of Rice Flour. This is one of the healthiest sweets in Japan!",
"Japanese assorted stews. Made from many different kind of veggetables. Popular in Winter!", //5
};
private Integer[] imgid = {
R.drawable.thumb_classic, R.drawable.thumb_side, R.drawable.thumb_reverse, R.drawable.thumb_swissball, R.drawable.thumb_reachout, //5
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easylist);
mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for(int i=0;i<title.length;i++){
try {
rd = new RowData(i,title[i],description[i]);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.list_item_main, android.R.id.list, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
private void setContentView(int main) {
// TODO Auto-generated method stub
}
private LayoutInflater getSystemService(String layoutInflaterService) {
// TODO Auto-generated method stub
return null;
}
public void onListItemClick(ListView listView, View view, int position, long id) {
Bundle bundle = new Bundle();
bundle.putInt("Layout", position);
Intent newIntent = new Intent(this.getApplicationContext(), ContentViewer.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
}
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
private class RowData {
protected int mId;
protected String mTitle;
protected String mDescription;
RowData(int id, String title, String description){
mId=id;
mTitle = title;
mDescription = description;
}
@Override
public String toString() {
return mId+" "+mTitle+" "+mDescription;
}
}
private class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource, int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
TextView description = null;
ImageView thumbnail = null;
RowData rowData = getItem(position);
if(null == convertView){
convertView = mInflater.inflate(R.layout.list_item_main, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.gettitle();
title.setText(rowData.mTitle);
description = holder.getdescription();
description.setText(rowData.mDescription);
thumbnail = holder.getImage();
thumbnail.setImageResource(imgid[rowData.mId]);
return convertView;
}
private class ViewHolder {
private View mRow;
private TextView title = null;
private TextView description = null;
private ImageView thumbnail = null;
public ViewHolder(View row) {
mRow = row;
}
public TextView gettitle() {
if(null == title){
title = (TextView) mRow.findViewById(R.id.title);
}
return title;
}
public TextView getdescription() {
if(null == description){
description = (TextView) mRow.findViewById(R.id.description);
}
return description;
}
public ImageView getImage() {
if(null == thumbnail){
thumbnail = (ImageView) mRow.findViewById(R.id.thumbnail);
}
return thumbnail;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法确定这是否是我遇到此问题时解决问题的原因,但我认为您没有正确遵循片段生命周期(参见 此处)。
在讨论活动时,您必须先创建
ListView
,然后再进行诸如setAdapter()
之类的调用。您正在onCreate()
中实现,该方法在onCreateView()
之前调用。因此,看起来您实际上是在创建
ListView
适配器之前尝试设置它。作为一种习惯,最好在片段生命周期的
onCreateView()
部分中使用视图来完成所有工作。I cannot remember for sure if this is what solved this problem when I had it, but I don't think you are correctly following the fragment lifecycle (seen here).
In the context of talking about an activity, you must first create your
ListView
before making calls likesetAdapter()
. You are implementing inonCreate()
which is called beforeonCreateView()
.So it simply seems like you are in essence trying to set the adapter of your
ListView
before you have created it.As a habit, it is always best to do all of your work with views within the
onCreateView()
part of the fragment lifecycle.