gpt4 book ai didi

android - 抽屉导航列表项在滚动时隐藏/不可见

转载 作者:行者123 更新时间:2023-11-30 02:27:54 26 4
gpt4 key购买 nike

我一直在使用一个在抽屉导航中显示项目列表的 Android 应用程序。抽屉包含标题和它们下面的内容。我的问题是,当我第一次启动应用程序时,抽屉列表中的所有内容看起来都很好。但是当我开始连续上下滚动时,屏幕顶部和底部的某些列表项被隐藏或不可见。我附上了相同的必要代码。请有人帮我解决这个问题。

自定义抽屉适配器类:

    public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem> {
Context context;
List<DrawerItem> drawerItemList;
int layoutResID;
Runnable r=null;
Handler handler;
View view;
public CustomDrawerAdapter(Context context, int layoutResourceID,
List<DrawerItem> listItems) {
super(context, layoutResourceID, listItems);
this.context = context;
this.drawerItemList = listItems;
this.layoutResID = layoutResourceID;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final DrawerItemHolder drawerHolder;
view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.ItemName = (TextView) view
.findViewById(R.id.drawer_itemName);
drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);
//drawerHolder.submit=(LinearLayout)view.findViewById(R.id.submit_layout);
// drawerHolder.submit_btn=(Button)view.findViewById(R.id.submit_btn);
/*drawerHolder.submit_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("SUBMIT","SUBMIT CLICKED");
Toast.makeText(getContext(),"SUBMITTED OPTION",Toast.LENGTH_SHORT).show();
}
});*/
drawerHolder.itemlayout=(LinearLayout)view.findViewById(R.id.itemlayout);
drawerHolder.header_icon=(ImageView)view.findViewById(R.id.header_icon);
drawerHolder.title = (TextView) view.findViewById(R.id.header_title);
drawerHolder.headerLayout = (LinearLayout) view
.findViewById(R.id.headerLayout);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
final DrawerItem dItem = this.drawerItemList.get(position);
if (dItem.getTitle() != null && drawerHolder.header_icon!=null) {
drawerHolder.headerLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.itemlayout.setVisibility(View.GONE);
drawerHolder.title.setText(dItem.getTitle());
//drawerHolder.submit.setVisibility(View.GONE);
} else {
drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(dItem.getImgResID()));
drawerHolder.header_icon.setVisibility(View.GONE);
drawerHolder.headerLayout.setVisibility(LinearLayout.GONE);
drawerHolder.ItemName.setText(dItem.getItemName());
//drawerHolder.submit.setVisibility(View.GONE);
}
/* else if(dItem.getTitle().contains("button"))
{
drawerHolder.headerLayout.setVisibility(LinearLayout.GONE);
drawerHolder.itemlayout.setVisibility(View.GONE);
//drawerHolder.submit.setVisibility(View.VISIBLE);
}*/
return view;
}
private static class DrawerItemHolder {
TextView ItemName, title;
ImageView icon,header_icon;
LinearLayout headerLayout,itemlayout;
LinearLayout submit;
//Button submit_btn;
}
}

抽屉项目类:

public class DrawerItem {
String ItemName;
int imgResID;
public int getHeader_image() {
return header_image;
}
public void setHeader_image(int header_image) {
this.header_image = header_image;
}
int header_image;
String title;
boolean isSpinner;
public DrawerItem(String itemName, int imgResID) {
super();
ItemName = itemName;
this.imgResID = imgResID;
}
public DrawerItem(String title) {
this(null, 0);
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isSpinner() {
return isSpinner;
}
public String getItemName() {
return ItemName;
}
public void setItemName(String itemName) {
ItemName = itemName;
}
public int getImgResID() {
return imgResID;
}
public void setImgResID(int imgResID) {
this.imgResID = imgResID;
}
}

我正在调用和绘制抽屉导航列表中的项目,如我的主页类中的以下代码 fragment 所示:

//--------------------NAVIGATION DRAWER ITEMS-----------------------------//
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(android.R.color.background_light,
GravityCompat.START);
dataList = new ArrayList<DrawerItem>();
String[] options = getResources().getStringArray(R.array.options);
dataList.add(new DrawerItem("Select your stylist", R.drawable.ic_stylist_select));
dataList.add(new DrawerItem("All Stylist"));
/*---------------STYLIST ARRAY NAVIGATION DRAWER-------------------------*/
stylistnamearray = new ArrayList<>();
for (int i = 0; i < jObj.length(); i++) {
JSONObject object = null;
try {
object = jObj.getJSONObject(i);
String stylistname = object.getString("StylistName");
stylistnamearray.add(stylistname);
set = new HashSet<>();
set.addAll(stylistnamearray);
} catch (JSONException e) {
e.printStackTrace();
}
}
stylistarray = set.toArray(new String[set.size()]);
for (int i = 0; i < set.size(); i++) {
dataList.add(new DrawerItem(stylistarray[i]));
Log.e("STYLIST ARRAY LIST", stylistarray[i]);
}
dataList.add(new DrawerItem("Select your location", R.drawable.ic_globe));
/*-------------------------LOCATION ARRAY NAVIGATION DRAWER--------------------------*/
location_arraylist = new ArrayList<>();
for (int i = 0; i < jObj.length(); i++) {
JSONObject object = null;
try {
object = jObj.getJSONObject(i);
String stylistname = object.getString("Location");
location_arraylist.add(stylistname);
set = new HashSet<>();
set.addAll(location_arraylist);
} catch (JSONException e) {
e.printStackTrace();
}
}
locationarray = set.toArray(new String[set.size()]);
for (int i = 0; i < set.size(); i++) {
dataList.add(new DrawerItem(locationarray[i]));
Log.e("LOCATION ARRAY LIST", locationarray[i]);
}
dataList.add(new DrawerItem("Select my designer", R.drawable.ic_stylist));
dataList.add(new DrawerItem("Wanna be a stylist ?", R.drawable.ic_stylist));
dataList.add(new DrawerItem("Hi Marya !", R.drawable.ic_call));
mDrawerList.bringToFront();
mDrawerLayout.requestLayout();
mDrawerList.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if (scrollState == SCROLL_STATE_IDLE) {
mDrawerList.bringToFront();
mDrawerLayout.requestLayout();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});

最后是我的 xml 布局(供引用):

主页 xml 布局:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/black"
android:dividerHeight="1dp"
android:background="#ddd8ba">
</ListView>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</android.support.v4.widget.DrawerLayout>

自定义抽屉项目 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0"
android:background="#ddd8ba">
<LinearLayout
android:id="@+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="left"
android:background="@drawable/list_item_selector"
android:orientation="horizontal">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/header_icon"
android:padding="2dp"/>
<TextView
android:id="@+id/header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:padding="10dp"
android:textColor="#000"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="15dp"
android:textStyle="bold" />
</LinearLayout>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tablerow"
android:background="#244242"
android:orientation="vertical">
<LinearLayout
android:id="@+id/itemlayout"
android:layout_width="fill_parent"
android:layout_span="1"
android:background="@drawable/reverse_list_item_selector"
android:layout_height="40dp"
android:layout_below="@+id/headerLayout">
<ImageView
android:id="@+id/drawer_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@android:color/transparent"
android:padding="5dp" />
<TextView
android:id="@+id/drawer_itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="10dp"
android:text="KRISH"
android:textColor="@color/ivorycream_background"
android:textSize="14dp" />
</LinearLayout>
</TableRow>
</TableLayout>

提前谢谢大家。如果您需要任何进一步的信息,请告诉我。

最佳答案

再次检查您的 CustomDrawerAdapter 类和 ListView。以下方法对我有用。

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

if (view == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
view = mInflater.inflate(R.layout.your_custom_drawer_item, null);
}

// use View, not LinearLayout
View navigationDrawerItemLayout = view.findViewById(R.id.itemLayout);
View navigationDrawerHeaderLayout = view.findViewById(R.id.headerLayout);

// for item layout. It based on your "if (itemCondition != null)"
if ((dItem.getTitle() != null) && (drawerHolder.your_item != null)) {
// It means, all of navigationDrawerItemLayout's contains are set to VISIBLE
navigationDrawerItemLayout.setVisibility(View.VISIBLE);
// It means, all of navigationDrawerHeaderLayout's contains are set to GONE
navigationDrawerHeaderLayout.setVisibility(View.GONE);
}

// For header layout. It based on your "if (headerCondition != null)"
if ((dItem.getTitle() != null) && (drawerHolder.your_header != null)) {
// It means, all of navigationDrawerItemLayout's contains are set to GONE
navigationDrawerItemLayout.setVisibility(View.GONE);
// It means, all of navigationDrawerHeaderLayout's contains are set to VISIBLE
navigationDrawerHeaderLayout.setVisibility(View.VISIBLE);
}

return view;

在您的 ListView 中:

<ListView
android:id="@+id/left_drawer"
android:background="@color/your_list_background"
android:cacheColorHint="#00000000"
android:scrollbarFadeDuration="0" />

关于android - 抽屉导航列表项在滚动时隐藏/不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27683782/

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