Android滚动,带行单击的多列ListView

时间:2017-04-07 02:42:19

标签: java android listview

经过几天寻找解决方案...... 我实现了一个滚动,4列ListView与行单击,只消耗我的应用程序的主屏幕的中间部分。这正是我的应用程序所需要的,并且非常接近完美。屏幕截图

Listview looking good

唯一的问题是这个。当我滚动列表时,行变得不顺序并随机改变序列。每行中的列保持一致。但这些行似乎在移动。下面的屏幕截图显示不按顺序。 "测序问题"是动态的。意思是,滚动越多,序列变得越随机。

Rows get out of sequence on scroll

我看过listview被认为是老式的,有大型数据集等问题,但我一直无法找到任何其他支持4列,滚动,不占整个屏幕,行点击等等。上面的数据集只有20-50行。

需要帮助: 1 - 有没有办法解决上面的行排序问题?如果是,是否还有一种方法可以在我上面的当前列表视图中引入网格线? 2 - 如果不是,我怎么能用像RecyclerView,TableLayout等更新的东西来获得相同的视觉效果。

这是我的代码:

activity_main.xml中

<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="com.example.ftonyan.listviewmulticolumns.MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="350dp"
        android:layout_height="150dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="153dp"></ListView>

</RelativeLayout>

colmn_row.xml

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

    <TextView
        android:id="@+id/name"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.2"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/gender"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.35" />

    <TextView
        android:id="@+id/age"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.5" />

    <TextView
        android:id="@+id/status"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1.5" />

</LinearLayout>

ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter {

    public ArrayList<HashMap<String, String>> list;
    Activity activity;
    TextView txtFirst;
    TextView txtSecond;
    TextView txtThird;
    TextView txtFourth;
    public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
        super();
        this.activity=activity;
        this.list=list;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater=activity.getLayoutInflater();
        if(convertView == null){
            convertView=inflater.inflate(R.layout.colmn_row, null);
            txtFirst=(TextView) convertView.findViewById(R.id.name);
            txtSecond=(TextView) convertView.findViewById(R.id.gender);
            txtThird=(TextView) convertView.findViewById(R.id.age);
            txtFourth=(TextView) convertView.findViewById(R.id.status);
        }
        HashMap<String, String> map=list.get(position);
        txtFirst.setText(map.get(FIRST_COLUMN));
        txtSecond.setText(map.get(SECOND_COLUMN));
        txtThird.setText(map.get(THIRD_COLUMN));
        txtFourth.setText(map.get(FOURTH_COLUMN));
        return convertView;
    }
}

MainActivity.java

public class MainActivity extends Activity {


    private ArrayList<HashMap<String, String>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String localFormatMinSec = "HH:mm:ss";
        SimpleDateFormat localSDFMinSec = new SimpleDateFormat(localFormatMinSec);

        ListView listView=(ListView)findViewById(R.id.listView1);

        list=new ArrayList<HashMap<String,String>>();

        if (1 == 1) {

            for (int i = 0; i < 14; i++) {
                HashMap<String, String> temp2 = new HashMap<String, String>();
                temp2.put(FIRST_COLUMN, String.valueOf(master.slave.get(i).getSequence()));
                temp2.put(SECOND_COLUMN, master.slave.get(i).getName());
                temp2.put(THIRD_COLUMN, localSDFMinSec.format(master.slave.get(i).getEstEnd()));
                temp2.put(FOURTH_COLUMN, master.slave.get(i).getMessage());
                list.add(temp2);
            }

        }

        ListViewAdapter adapter=new ListViewAdapter(this, list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
            {
                int pos=position+1;
                Toast.makeText(MainActivity.this, Integer.toString(pos)+" Clicked", Toast.LENGTH_SHORT).show();
            }

        });

    }

}

0 个答案:

没有答案