简体   繁体   中英

List to individual item swipe

I have an activity that returns a list of information derived from Parse.com in a listview. I would want that each item on the list is shown individually on a page, and where user are able to swipe left or right or navigate through arrows left and right through the list to view other list item.

Below is the code for that produces the list:

public class MatchingActivity extends Activity {

     private String currentUserId;
        private ArrayAdapter<String> namesArrayAdapter;
        private ArrayList<String> names;
        private ListView usersListView;
        private Button logoutButton;

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

            logoutButton = (Button) findViewById(R.id.logoutButton);
            logoutButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ParseUser.logOut();
                    Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                    startActivity(intent);
                }
            });

            setConversationsList();
        }

        private void setConversationsList() {
            currentUserId = ParseUser.getCurrentUser().getObjectId();
            names = new ArrayList<String>();

            ParseQuery<ParseUser> query = ParseUser.getQuery();
            query.whereNotEqualTo("objectId", currentUserId);
            query.findInBackground(new FindCallback<ParseUser>() {
                public void done(List<ParseUser> userList, ParseException e) {
                    if (e == null) {
                        for (int i=0; i<userList.size(); i++) {
                            names.add(userList.get(i).getUsername().toString());
                        }

                        usersListView = (ListView)findViewById(R.id.usersListView);
                        namesArrayAdapter =
                            new ArrayAdapter<String>(getApplicationContext(),
                                R.layout.user_list_item, names);
                        usersListView.setAdapter(namesArrayAdapter);

                        usersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> a, View v, int i, long l) {
                                openConversation(names, i);
                            }
                        });

                    } else {
                        Toast.makeText(getApplicationContext(),
                            "Error loading user list",
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
        }

        public void openConversation(ArrayList<String> names, int pos) {
            ParseQuery<ParseUser> query = ParseUser.getQuery();
            query.whereEqualTo("username", names.get(pos));
            query.findInBackground(new FindCallback<ParseUser>() {
               public void done(List<ParseUser> user, ParseException e) {
                   if (e == null) {
                       Intent intent = new Intent(getApplicationContext(), MessagingActivity.class);
                       intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
                       startActivity(intent);
                   } else {
                       Toast.makeText(getApplicationContext(),
                           "Error finding that user",
                               Toast.LENGTH_SHORT).show();
                   }
               }
            });
        }
    }

The XML layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center">

   <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/logout"
        android:id="@+id/logoutButton"
        android:gravity="center_vertical|center_horizontal"
        android:layout_gravity="center_horizontal"
        android:background="@color/dark_gray"
        android:textSize="24sp"
        android:padding="15dp"
        android:textColor="@color/sinch_purple" />

    <ListView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
       android:textColor="#ffffff"
        android:background="@drawable/bac_blue"
        android:id="@+id/usersListView">
        </ListView>

</RelativeLayout>

I have also have looked into http://developer.android.com/training/implementing-navigation/lateral.html , but not sure how I would incorporate that into my listview.

If you require additional information, let me know.

尝试使用Viewpager, 这里有示例和文档。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM