问题描述:

I want to run a sample project of StickyListHeaders in Eclipse. For that I copy-pasted all their classes,layouts,etc. into my project but into that code many errors are generated !

As the code is so large & there are so much errors that I don't feel like questioning here.

And I don't find any tutorial like it on google & also in SO, the questions about StickyListHeaders are subjected to some/other problem.

Anyone can provide me a demo app (tutorial) implementing StickyListHeaders that runs.

Sorry, If my question doesn't show any research effort or If you think that I am forcing someone to do some work !

Thanks..

网友答案:

You can also use below code to achieve StickyListHeaders,

MainActivity.java

public class MainActivity extends Activity implements AbsListView.OnScrollListener {
     ListView list; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_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.activity_main, 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.activity_main, 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;
    }


}

As you can see above android's AbsListView.OnScrollListener interface is used by implementing its onScroll() to listen the scrolling of the list.onScroll()will be called after the scroll has completed.There is an adapter which uses ViewHolder to store the header view and also a variable previousTop to keep track of the change in the scroll for each successive scroll events .This is because of the fact that offsetTopAndBottom() changes offset of the view related to the previous location of it.

Coming to the Adapter class, add(Integer.toString(i)); is used just to add integer values to an ArrayList which in turn used to show as a header. As a result you can see 50 headers in the ListView. void android.widget.ArrayAdapter.add(String object) Adds the specified object at the end of the array.

fragment_main.xml

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

The xml below contains a TextView which is for Header and an ImageView which is for the content part.You can use any view for it as per your need.

activity_main.xml

<?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/ic_launcher"
        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>

The code is pretty self explanatory.Still if you need any explanation, I will explain.

You can also refer the following,

http://sriramramani.wordpress.com/2012/10/17/instagram-list-view/

https://bitbucket.org/ashish2py/instagramheader/downloads

Note: I have copied this code from somewhere I don't remember. If anyone can provide me the link to this code, I will add the reference here.Sorry for not giving the credit to the original author.

相关阅读:
Top