我无法在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_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:id="@+id/avatar"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:weightSum="2"
android:orientation="vertical">
<TextView
android:id="@+id/titolo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="@color/black"
android:text="ciao"/>
<TextView
android:id="@+id/descrizione"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5"
android:text="ciaociao"/>
</LinearLayout>
答案 0 :(得分:0)
在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;
}