本文介绍了填充ListFragments以自定义视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! previously,我没有问题,用充气的布局,作为一个ListView自定义视图层次结构。但我对如何为listFragment做同样的毫无头绪。可以说我有与ImageView的和2 textviews在它的item_list布局。我想夸大这在我ListFragment使用。但如何? 公共类Tab1Fragment扩展ListFragment {私人LayoutInflater mInflater;私人矢量&lt; RowData&GT;数据;RowData次; 静态最终的String []标题=新的String [] { 经典的木板,侧栈道,反普朗克,Swissball普朗克,一只胳膊一条腿,// 5 };静态最终的String []说明=新的String [] { 变异寿司的新鲜蔬菜和海鲜!适合家庭场合!, 亲子丼是日本饭碗菜,鸡肉,鸡蛋和各种各样的健康美味Veggetables!, 这是来自许多不同的食材烹制的日本什锦煎饼的味道太好吃了!, 做米粉的日本饺子。这是最健康的甜食在日本的一个! 日本什锦炖菜。从许多不同类型的veggetables的,受欢迎的国产冬天!,// 5 };私人整数[] imgid = { R.drawable.thumb_classic,R.drawable.thumb_side,R.drawable.thumb_reverse,R.drawable.thumb_swissball,R.drawable.thumb_reachout,// 5}; @覆盖 公共无效的onCreate(包savedInstanceState){super.onCreate(savedInstanceState);的setContentView(R.layout.easylist);mInflater =(LayoutInflater)getSystemService(Activity.LAYOUT_INFLATER_SERVICE);数据=新的矢量&lt; RowData&GT;();的for(int i = 0; I&LT; title.length;我++){ 尝试 { 次=新RowData(ⅰ,标题[I]中,描述[I]); }赶上(ParseException的E){ e.printStackTrace(); } data.add(RD); }CustomAdapter适配器=新CustomAdapter(这一点,R.layout.list_item_main,android.R.id.list,数据); setListAdapter(适配器); getListView()setTextFilterEnabled(真)。}私人无效的setContentView(INT主){// TODO自动生成方法存根}私人LayoutInflater getSystemService(字符串layoutInflaterService){// TODO自动生成方法存根返回null;}公共无效onListItemClick(ListView控件的ListView,视图中查看,INT位置,长的id){束束=新包();bundle.putInt(布局,位置);意图newIntent =新的意图(this.getApplicationContext(),ContentViewer.class);newIntent.putExtras(包);startActivityForResult(newIntent,0);} 民营背景getApplicationContext(){// TODO自动生成方法存根返回null;}私有类RowData { 保护INT MID; 保护字符串mTitle; 保护字符串mDescription; RowData(INT ID,字符串名称,字符串描述){ MID = ID; mTitle =称号; mDescription =描述;} @覆盖 公共字符串的toString(){ 返回MID ++ mTitle ++ mDescription; }} 私有类CustomAdapter扩展ArrayAdapter&LT; RowData&GT; { 公共CustomAdapter(上下文的背景下,INT资源,INT textViewResourceId,名单,其中,RowData&GT;对象){ 超(背景下,资源,textViewResourceId,对象);}@覆盖 公共查看getView(INT位置,查看convertView,ViewGroup中父){ ViewHolder支架=无效; TextView的标题= NULL; TextView中描述= NULL; ImageView的缩略图= NULL; RowData rowData =的getItem(位置); 如果(空== convertView){ convertView = mInflater.inflate(R.layout.list_item_main,NULL); 持有人=新ViewHolder(convertView); convertView.setTag(保持器); } 支架=(ViewHolder)convertView.getTag(); 标题= holder.gettitle(); title.setText(rowData.mTitle); 说明= holder.getdescription(); description.setText(rowData.mDescription); 缩略图= holder.getImage(); thumbnail.setImageResource(imgid [rowData.mId]); 返回convertView;} 私有类ViewHolder { 私人查看MROW; 私人TextView的标题= NULL; 私人TextView中描述= NULL; 私人ImageView的缩略图= NULL; 公共ViewHolder(查看行){ MROW =行; } 公众的TextView的getTitle(){ 如果(空==标题){ 标题=(TextView中)mRow.findViewById(R.id.title); } 返回称号; } 公众的TextView getdescription(){ 如果(空==介绍){ 说明=(TextView中)mRow.findViewById(R.id.description); } 返回描述; } 公共ImageView的的getImage(){ 如果(空==缩略图){ 缩略图=(ImageView的)mRow.findViewById(R.id.thumbnail); } 返回缩略图; } }}}解决方案 我不记得是肯定的,如果这是解决了这个问题,当我有,但我不认为你是正确的以下片段生命周期(见于这里)。在谈论活动的背景下,您必须首先创建您的的ListView 作出这样的方法调用之前的 setAdapter()。您正在实施的onCreate()这是之前名为 onCreateView()所以它只是好像你在本质上试图设置的适配器您的的ListView 您已经创建了它。作为一种习惯,它始终是最好的范围内完成所有的工作,享有 onCreateView()片段生命周期的一部分。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 stubreturn 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 stubreturn 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; } }}} 解决方案 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 like setAdapter(). You are implementing in onCreate() which is called before onCreateView().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. 这篇关于填充ListFragments以自定义视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-08 18:54