Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Listview自定义节头_Android_Listview_Customization - Fatal编程技术网

Android Listview自定义节头

Android Listview自定义节头,android,listview,customization,Android,Listview,Customization,如何在android中实现类似Instagram应用程序的ListView自定义节或标题 当向上滚动包含userpic的条时,名称和时间仍然存在,当其他标题条靠近它时,则会像向上推一样设置动画 谢谢。如果您使用的是列表视图,则列表项中有两个视图。一个是标题,另一个实际上是项目 最初隐藏标题视图,并在滚动状态更改或用户触摸列表项时更改标题的可见性 在setcontentview之后,执行类似以下代码的操作 ListView list = getListView(); View f

如何在android中实现类似Instagram应用程序的ListView自定义节或标题

当向上滚动包含userpic的条时,名称和时间仍然存在,当其他标题条靠近它时,则会像向上推一样设置动画


谢谢。

如果您使用的是列表视图,则列表项中有两个视图。一个是标题,另一个实际上是项目


最初隐藏标题视图,并在滚动状态更改或用户触摸列表项时更改标题的可见性

在setcontentview之后,执行类似以下代码的操作

    ListView list = getListView();
    View footer = getLayoutInflater().inflate(R.layout.footerlayout, list, false);
    View header = getLayoutInflater().inflate(R.layout.headerlayout, list, false);
    list.addHeaderView(header);
    list.addFooterView(footer); 

我可以通过在listview上使用滚动监听器来解决这个问题。(在2.1中测试)

假设对于每个列表行,我都有一个如下所示的布局。有一个内容部分和一个标题部分。无论您对标题或内容使用何种视图类型

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:background="#FFFFFF">

    <ImageView
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:scaleType="centerCrop" 
        android:src="@drawable/pic"
        android:background="#aaaaff" 
        android:layout_marginTop="40dp"/>

    <TextView
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:padding="12dp"
        android:text="Deneme Row"
        android:textColor="#000000" 
        android:background="#99ffffff"/>
</RelativeLayout>

活动的测试布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>
</LinearLayout>

最后,下面给出了该活动的代码。这里我必须有一个适配器,它使用ViewHolder来存储标题视图,还有一个变量来跟踪每个连续滚动事件(previousTop)的滚动变化。这是因为OffsetOpandBottom()更改了视图相对于其上一个位置的偏移量

public class TestActivity extends Activity implements AbsListView.OnScrollListener{

    ListView list; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list = (ListView) findViewById(R.id.list);
        list.setAdapter(new Adapter(this));        
        list.setOnScrollListener(this); 
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {     
        //the listview has only few children (of course according to the height of each child) who are visible
        for(int i=0; i < list.getChildCount(); i++){
            View child = list.getChildAt(i);
            ViewHolder holder = (ViewHolder) child.getTag();

            //if the view is the first item at the top we will do some processing
            if(i == 0){             
                boolean isAtBottom = child.getHeight() <= holder.header.getBottom();
                int offset = holder.previousTop - child.getTop();
                if(!(isAtBottom && offset > 0)){                    
                    holder.previousTop = child.getTop();
                    holder.header.offsetTopAndBottom(offset);                   
                    holder.header.invalidate();
                }
            } //if the view is not the first item it "may" need some correction because of view re-use
            else if (holder.header.getTop() != 0) {
                int offset = -1 * holder.header.getTop(); 
                holder.header.offsetTopAndBottom(offset);
                holder.previousTop = 0;
                holder.header.invalidate();
            }
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {}

    private static class Adapter extends ArrayAdapter<String> {
        public Adapter(Context context) {
            super(context, R.layout.row, R.id.header);
            for(int i=0; i < 50; i++){
                add(Integer.toString(i));
            }
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
                ViewHolder holder = new ViewHolder();
                holder.header = (TextView) convertView.findViewById(R.id.header);
                convertView.setTag(holder);             
            }
            ViewHolder holder = (ViewHolder) convertView.getTag();
            holder.header.setText(getItem(position));
            return convertView;
        }
    }

    private static class ViewHolder {
        TextView header;
        int previousTop = 0;
    }
}
公共类TestActivity扩展活动实现AbsListView.OnScrollListener{
列表视图列表;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list=(ListView)findViewById(R.id.list);
list.setAdapter(新适配器(此));
list.setOnScrollListener(此);
}
@凌驾
public void onScroll(AbsListView视图,int firstVisibleItem,
int visibleItemCount,int totalItemCount){
//listview只有几个可见的子对象(当然,根据每个子对象的高度)
对于(int i=0;i
我对Siyamed的问题做了一些改进,以便在重画视图时(例如,如果listview项中的位图发生更改),标题不会消失

我没有使用相对于最后一个位置的坐标,而是使用相对于视图顶部的坐标,并使用填充而不是偏移

@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    for(int i=0; i < list.getChildCount(); i++){
        View child = list.getChildAt(i);
        ViewHolder holder = (ViewHolder) child.getTag();

        if(i == 0){
            try {
                boolean isAtBottom = child.getHeight() <= holder.movingHeader.getBottom();
                if(!(isAtBottom)){
                    if (child.getTop() >= 0){}
                    else if (child.getHeight() - movingHeader.getHeight() - 1 > -child.getTop())
                    {
                        holder.movingHeader.setPadding(0, -child.getTop(), 0, 0);
                        holder.movingHeader.invalidate();
                    }
                    else {
                        holder.movingHeader.setPadding(0, child.getHeight() - movingHeader.getHeight(), 0, 0);
                        holder.movingHeader.invalidate();
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        else if (holder.movingHeader.getPaddingTop() != 0)
        {
            holder.movingHeader.setPadding(0, 0, 0, 0);
            holder.movingHeader.invalidate();
        }
    }
}
@覆盖
public void onScroll(AbsListView AbsListView、int firstVisibleItem、int visibleItemCount、int totalItemCount){
对于(int i=0;i-child.getTop())
{
holder.movingHeader.setPadding(0,-child.getTop(),0,0);
holder.movingHeader.invalidate();
}
否则{
holder.movingHeader.setPadding(0,child.getHeight()-movingHeader.getHeight(),0,0);
holder.movingHeader.invalidate();
}
}
}
捕获(例外e)
{
e、 printStackTrace();
}
}
else if(holder.movingHeader.getPaddingTop()!=0)
{
固定器。移动头。设置填充(0,0,0,0);
holder.movingHeader.invalidate();
}
}
}

我知道这个问题有点老了,但还是找到了StickyListHeaders库,它在抽象节标题的创建方面做得相当好


嘿,西亚迈德,这很有用。在我的例子中,ListView(父)中还有一个ListView(子)。所以,当我向下滚动时,标题无法正常工作…如果子listview中没有数据,则它可以正常工作..>样品