Android - Using SwipeView for an entire ListView

2019-09-02 01:53发布

I've seen several examples (here) of SwipeViews that are implemented on a ListView, but these are for individual items on the list and not for the ListView itself.

In my app, an intent is started from one Listview item and that sends you to the Listview in question. From there, I would like to implement some kind of function that allows me to swipe from one ListView to another. Drilling down with intents all the time would not provide as user-friendly an experience as SwipeView would, in this instance.

If I haven't explained this clearly, please let me know. If I have to create extra classes and XML layout files to do this, then so be it.

CustomListAdapter.java (The class which holds the ListView code)

public class CustomListAdapter extends ArrayAdapter<String> {
    private Context mContext;
    private int id;
    private ArrayList<String> callData;
    private String[] headers = {"Number", "Customer Number", "Name", "Bill to Customer Number", "Bill to Name",
            "Order Date", "Order Time", "Response Date", "Response Time", "Fix by Date", "Fix by Time",
            "Responded Date", "Responded Time", "Fixed Date", "Fixed Time", "Your Reference",
            "Description", "Transaction Status", "Ship to Code", "Ship to Name", "Ship to Address",
            "Ship to Address 2", "Ship to City", "Ship to County", "Ship to Postcode", "Contact Name",
            "Phone Number", "Note", "Sources"};

    public CustomListAdapter(Context context, int textViewResourceId, ArrayList<String> callData) {
        super(context, textViewResourceId, callData);
        this.callData = callData;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_service_order, null);
        }

        String test = callData.get(position);

        if (test != null) {
            TextView customerNoLabel = (TextView) view.findViewById(R.id.name_label);
            TextView customerNoData = (TextView) view.findViewById(R.id.listed_data);

            if (customerNoLabel != null) {
                customerNoLabel.setText(headers[position]);
                customerNoData.setText(callData.get(position));
            }
        }
        return view;
    }
}

CallActivity2.java (The class that implements the code for the CustomListAdapter and which I want to use to swipe to two different listview with different headings and values)

    public class CallActivity2 extends ListActivity {
    private ArrayList<String> individualCallData;
   // private ArrayAdapter<String> callDataAdapter;
    private CustomListAdapter callDataAdapter;
    private ListView callDataView;
    private Runnable viewParts;
    private TextView serviceOrderNo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.call_display);

        Intent intent = getIntent();
        individualCallData = intent.getStringArrayListExtra("Call");

        // Instantiate CustomListAdapter class
        callDataView = (ListView) findViewById(android.R.id.list);
        callDataAdapter = new CustomListAdapter(this, R.layout.list_service_order, individualCallData);
        setListAdapter(callDataAdapter);



        viewParts = new Runnable(){
            public void run(){
                handler.sendEmptyMessage(0);
            }
        };

        // here we call the thread we just defined - it is sent to the handler below.
        Thread thread =  new Thread(null, viewParts, "MagentoBackground");
        thread.start();


    }

    private Handler handler = new Handler() {
        public void handleMessage(Message msg)
        {
            // create some objects
            // here is where you could also request data from a server
            // and then create objects from that data.
            //individualCallData.add("Test");

            callDataAdapter = new CustomListAdapter(CallActivity2.this, R.layout.list_service_order, individualCallData);

            // display the list.
            setListAdapter(callDataAdapter);

        }
    };
}

3条回答
霸刀☆藐视天下
2楼-- · 2019-09-02 02:52

Instead, I decided to get rid of the ListView and use ScrollView instead. There is more flexibility in what I can do. Furthermore, I can scroll and swipe, too. Listview doesn't do that and it is not an item click. Thank you for the two guys that contributed though.

查看更多
放我归山
3楼-- · 2019-09-02 02:53

I recomend to use a library like this one https://github.com/xenione/SwipeLayout and don't try to do it on your own. AND please avoid to use ListView use RecyclerView instead.

查看更多
淡お忘
4楼-- · 2019-09-02 02:57

I think you are looking for a ViewPager. In which case you would not need to create more xml files (unless you want the list items in each list to look different), but you will need to create some Fragments. In this way, you can create a new Fragment for each list, and the ViewPager will handle the swipe gesture to go back and forth between the two.

查看更多
登录 后发表回答