sticky row in listview in activity

别说谁变了你拦得住时间么 提交于 2019-12-02 10:15:44

I wrote this code for Json... Hope this will solve your puzzle.. And you like this...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

        <TextView
            android:id="@+id/headerRow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="@string/hello_world" />

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/headerRow" >

        </ListView>

    </RelativeLayout>

    public class MainActivity extends Activity {

        private ArrayList<String> data;
        private TextView stickRow;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            stickRow = (TextView) findViewById(R.id.headerRow);
            data = new ArrayList<String>();
            for (int ak = 1; ak < 20; ak++) {
                data.add("Row " + ak);
            }

            ListView lv = (ListView) findViewById(R.id.listView1);
            lv.setAdapter(new CustomAdapter(getApplicationContext(), R.layout.activity_main));

        }

        class CustomAdapter extends ArrayAdapter<String> {

            public CustomAdapter(Context context, int resource) {
                super(context, resource);
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                if (position == 0) {
                    stickRow.setText(getItem(position));
                }
                TextView tv = new TextView(getApplicationContext());
                tv.setTextSize(20);
                tv.setText(getItem(position + 1));
                return tv;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return data.size() - 1;
            }

            @Override
            public String getItem(int position) {
                // TODO Auto-generated method stub
                return data.get(position);
            }

        }
    }

Its not possible with only ListView.

If I would be in your position I would have used same single item layout (which is going to be used in single item of ListView) as fixed layout, below that I would have my ListView.

<Layout1>
    <Layout2>
        <ItemView1>
        </ItemView1>
    </Layout2>
    <ListView>
    </ListView> 
</Layout1>

Layout1 and Layout2 could be Relative or Linear layout

ItemView1 is the same layout which is going to be used in ListView item layout.

As my first row is going to be constant I will set the values once the view is invoked and the rest will work as usual.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!