sticky row in listview in activity

后端 未结 2 1541
一向
一向 2021-01-27 19:21

Just one row in my listview should be sticky .Not sections or section with alphabets in stickyheaders.I really appreciate any help w.r.t listview sticky one row within activity

相关标签:
2条回答
  • 2021-01-27 19:50

    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);
                }
    
            }
        }
    
    0 讨论(0)
  • 2021-01-27 20:00

    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.

    0 讨论(0)
提交回复
热议问题