列出单个项目滑动

时间:2014-08-08 15:19:38

标签: java android android-layout android-activity parse-platform

我有一个活动,它返回列表视图中从Parse.com派生的信息列表。我希望列表中的每个项目都单独显示在页面上,并且用户可以向左或向右滑动或在列表中左右滑动以查看其他列表项。

以下是生成列表的代码:

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();
                   }
               }
            });
        }
    }

XML布局

<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>

我也调查了http://developer.android.com/training/implementing-navigation/lateral.html,但不确定如何将其纳入我的列表视图。

如果您需要其他信息,请与我们联系。

1 个答案:

答案 0 :(得分:0)

尝试使用Viewpager,Here您有示例和文档。