gpt4 book ai didi

android - ListView 不显示在 fragment 中

转载 作者:太空狗 更新时间:2023-10-29 14:09:05 25 4
gpt4 key购买 nike

我在我的 Android 应用程序中实现了来自左侧屏幕的自己的抽屉导航。问题是 ListView 它不可见,我不知道为什么。

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

@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;
}

最后这是我的 fragment 的布局:在顶部我有一个带有图像的标题和一个 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" />

最佳答案

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

super(context, R.layout.drawerlist_layout);

我错过了第三个参数,在本例中是 list 所以它应该是

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

关于android - ListView 不显示在 fragment 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30494776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com