导航抽屉列表项在滚动中隐藏/不可见 [英] Navigation Drawer list items hide/go invisible on scroll

查看:96
本文介绍了导航抽屉列表项在滚动中隐藏/不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been working with an android app that shows a list of items in the navigation drawer. The drawer contains both the headers and contents beneath them.My problem is, when I start the app for the first time, everything in the drawer list appears good. But when I start scrolling up and down continuously , some of the list items on the top and bottom of the screen is hidden or go invisible. I am attaching the necessary codes of the same. Please someone help me out on this.

自定义抽屉适配器类:

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

然后我从Homepage类的以下片段中调用并绘制了导航抽屉列表中的项目:

And I am calling and drawing the items of the navigation drawer list as given in the below snippet from my Homepage class :

//--------------------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布局(供参考):

And finally my xml layouts (for reference) :

首页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布局:

custom drawer item xml layout :

<?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>

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

Thanks everyone in advance. If you need any further information, pls let me know.

推荐答案

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

Check your CustomDrawerAdapter class and ListView again. Below methods worked for me.

@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" />

这篇关于导航抽屉列表项在滚动中隐藏/不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆