不要在 ListFragment 中显示 Arraylist 的列表视图
Posted
技术标签:
【中文标题】不要在 ListFragment 中显示 Arraylist 的列表视图【英文标题】:Don't show listview of Arralist in ListFragment 【发布时间】:2016-01-31 22:55:50 【问题描述】:我无法在 ViewPager 的 ListFragment 中显示 Arraylist 列表...为什么?片段布局已加载,但列表未显示...
提前感谢您的帮助
SettingActivity.java
public class SettingActivity extends FragmentActivity
List<Fragment> fragments;
private PagerAdapter mPagerAdapter;
private ViewPager mPager;
...
@Override
protected void onCreate(Bundle savedInstanceState)
...
fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this,TemiFragment.class.getName()));
fragments.add(Fragment.instantiate(this,ImpostazioniFragment.class.getName()));
fragments.add(Fragment.instantiate(this,InfoFragment.class.getName()));
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(),fragments);
mPager = (ViewPager) super.findViewById(R.id.pager);
mPager.setAdapter(this.mPagerAdapter);...
TemiFragment.java
public class TemiFragment extends ListFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
final LinearLayout pager = (LinearLayout)inflater.inflate(R.layout.fragment_temi,container,false);
ListView listaTemi = (ListView) pager.findViewById(android.R.id.list);
List<TemiLista> lista = new ArrayList<TemiLista>();
lista.add(new TemiLista(R.drawable.tema1, R.string.titolo_1, R.string.desc_1));
lista.add(new TemiLista(R.drawable.tema1, R.string.titolo_1, R.string.desc_1));
lista.add(new TemiLista(R.drawable.tema1, R.string.titolo_1, R.string.desc_1));
listaTemi.setAdapter(new TemiAdapter(this.getActivity(), lista));
return inflater.inflate(R.layout.fragment_temi,container,false);
TemiAdapter.java
public class TemiAdapter extends BaseAdapter
private Context context;
private List<TemiLista> lista;
ViewHolder holder;
public TemiAdapter(Context context, List<TemiLista> lista)
this.lista = lista;
this.context = context;
@Override
public int getCount()
return (lista == null) ? 0 : lista.size();
@Override
public Object getItem(int position)
return lista.get(position);
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
LayoutInflater inflater = LayoutInflater.from(context);
if(convertView == null)
convertView = inflater.inflate(R.layout.temi_inflate, parent, false);
holder = new ViewHolder();
holder.avatar = (ImageView)convertView.findViewById(R.id.avatar);
holder.titolo = (TextView)convertView.findViewById(R.id.titolo);
holder.descrizione = (TextView)convertView.findViewById(R.id.descrizione);
convertView.setTag(holder);
else
holder = (ViewHolder)convertView.getTag();
TemiLista riga = lista.get(position);
holder.avatar.setImageResource(riga.getImage());
holder.titolo.setText(riga.getTitolo());
holder.descrizione.setText(riga.getDescrizione());
return convertView;
static class ViewHolder
public ImageView avatar;
public TextView titolo, descrizione;
temi_inflate.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:id="@+id/avatar"
android:layout_
android:layout_
android:layout_weight="1"/>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="1"
android:weightSum="2"
android:orientation="vertical">
<TextView
android:id="@+id/titolo"
android:layout_
android:layout_
android:layout_weight="0.5"
android:background="@color/black"
android:text="ciao"/>
<TextView
android:id="@+id/descrizione"
android:layout_
android:layout_
android:layout_weight="1.5"
android:text="ciaociao"/>
</LinearLayout>
【问题讨论】:
【参考方案1】:在onCreateView
中返回inflated layout(pager)
而不是inflating new layout
。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
final LinearLayout pager = (LinearLayout)inflater.inflate(R.layout.fragment_temi,container,false);
ListView listaTemi = (ListView) pager.findViewById(android.R.id.list);
List<TemiLista> lista = new ArrayList<TemiLista>();
lista.add(new TemiLista(R.drawable.tema1, R.string.titolo_1, R.string.desc_1));
lista.add(new TemiLista(R.drawable.tema1, R.string.titolo_1, R.string.desc_1));
lista.add(new TemiLista(R.drawable.tema1, R.string.titolo_1, R.string.desc_1));
listaTemi.setAdapter(new TemiAdapter(this.getActivity(), lista));
return pager;
【讨论】:
非常感谢...你是最棒的:) @user2198593 很高兴为您提供帮助... :-)以上是关于不要在 ListFragment 中显示 Arraylist 的列表视图的主要内容,如果未能解决你的问题,请参考以下文章
ListFragment 仅在带有 ExpandableListadpater 的片段视图中显示第一项
列表项单击时 ListFragment 不会替换 ListFragment