ListView不显示在片段中

时间:2015-05-27 23:23:45

标签: android listview android-fragments android-arrayadapter navigation-drawer

我实现了自己的导航抽屉,它来自我的Android应用程序中的左侧屏幕。问题是ListView它不可见,我无法弄清楚原因。

这是列表所在的片段的onCreateView方法(首先我初始化布局的标题,然后使用我的自定义适配器初始化列表):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    View firstAccessView = inflater.inflate(R.layout.fragment_navigation_drawer, null);
    this.profileImageDrawer = (ImageView) firstAccessView.findViewById(R.id.imageViewProfileDrawer);
    RoundImage roundedImage = new RoundImage(BitmapFactory.decodeResource(getResources(), R.mipmap.iconuseranonymous));


    //this.profileImageDrawer.setImageDrawable(roundedImage);

    Picasso.with((Activity) getActivity()).load("https://graph.facebook.com/" + Profile.getCurrentProfile().getId() + "/picture?height=105&width=105")
            .placeholder(roundedImage)
               .transform(new CircleTransform()).fit().centerCrop().into(this.profileImageDrawer);
    this.nameSurnameDrawer = (TextView) firstAccessView.findViewById(R.id.textViewDrawner);
    this.nameSurnameDrawer.setText(Profile.getCurrentProfile().getFirstName() + " " + Profile.getCurrentProfile().getLastName());

    List<String> example = new ArrayList<String>();
    example.add("Profile");
    example.add("Ask");
    example.add("Logout");
    this.adapter = new DrawerAdapter(getActivity(), example);
    list = (ListView) firstAccessView.findViewById(R.id.listDrawer);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    });


    list.setItemChecked(mCurrentSelectedPosition,true);

    return firstAccessView;
}

这是我的自定义适配器的代码

 private Activity context;
private List<String> list;
private View view;
private TextView textView;

public DrawerAdapter(Activity context, List<String> list) {
    super(context, R.layout.drawerlist_layout);
    this.context=context;
    this.list = list;

}

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = context.getLayoutInflater();
    view = inflater.inflate(R.layout.drawerlist_layout, null);

    textView = (TextView) view.findViewById(R.id.sectionDrawer);
    textView.setText(this.list.get(position));
    textView.setTextColor(Color.BLACK);

    return view;
}

最后这是我的片段的布局:在顶部我有一个带有图片和TextView的标题然后我有我的自定义ListView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_weight="0.40"
    android:background="#ffb200">

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/imageViewProfileDrawer"
        android:layout_gravity="left|center_vertical"
        android:layout_marginLeft="20px" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Medium Text"
        android:id="@+id/textViewDrawner"
        android:layout_gravity="right|bottom"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="10dp"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:textIsSelectable="true" />

</FrameLayout>


<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="262dp" android:choiceMode="singleChoice"
    android:divider="#cecece" android:dividerHeight="1dp"
    android:background="#FFFFFF"
    tools:context="com.bellantoni.chetta.lieme.NavigationDrawerFragment"
    android:id="@+id/listDrawer"
    android:drawSelectorOnTop="false"
    android:smoothScrollbar="true"
    android:longClickable="false"
    android:nestedScrollingEnabled="false"
    android:footerDividersEnabled="false"
    android:headerDividersEnabled="true"
    android:paddingLeft="10dp"
    android:stackFromBottom="false"
    android:scrollingCache="false"
    android:paddingRight="10dp"
    android:textFilterEnabled="false"
    android:theme="@style/Base.Widget.AppCompat.ActionMode"
    android:textAlignment="center"
    android:transitionGroup="true"
    android:layout_weight="0.60" />

2 个答案:

答案 0 :(得分:1)

我在这行代码中发现了错误:

super(context, R.layout.drawerlist_layout);

我错过了第三个参数,在这种情况下是list所以它应该是

super(context, R.layout.drawerlist_layout,list);

答案 1 :(得分:0)

如果您想使用layout_weight,请将ListViewlayout_width 0dp设置为match_parent而不是{{1}}。