我想像下面的图片

时间:2016-07-27 13:36:57

标签: android listview

我想要像图像一样的列表视图 所以我可以有一个图像  整个项目并有一个按钮

enter image description here

2 个答案:

答案 0 :(得分:2)

您希望在列表视图中显示的图像,即列表视图中的项目只是一个布局。您可以设计自己的布局,您可以根据需要放置图像和按钮。

您需要为此实现自己的适配器,您可以使用自己定义的图像路径和Button文本或TextView文本在列表视图中创建每个新项目。

您可以使用自己的适配器为列表视图中的每个项目定义单独的图像和文本。

例如:

ArrayAdapter:

public class UsersAdapter extends ArrayAdapter<User> {
    public UsersAdapter(Context context, ArrayList<User> users) {
       super(context, 0, users);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       // Get the data item for this position
       User user = getItem(position);    
       // Check if an existing view is being reused, otherwise inflate the view
       if (convertView == null) {
          convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
       }
       // Lookup view for data population
       TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
       TextView tvHome = (TextView) convertView.findViewById(R.id.tvHome);
       // Populate the data into the template view using the data object
       tvName.setText(user.name);
       tvHome.setText(user.hometown);
       // Return the completed view to render on screen
       return convertView;
   }
}

用户类:

public class User {
    public String name;
    public String hometown;

    public User(String name, String hometown) {
       this.name = name;
       this.hometown = hometown;
    }
}

了解更多信息:https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

答案 1 :(得分:2)

使用cardview和recyclerview

创建cardAdapter

<强> CardAdapter.class

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
    private static Context context;
    View cardview;
    Cursor dataCursor;
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View view;
        public TextView title;
        public ImageView imageView;
        public ViewHolder(View v) {
            super(v);
            view = v;
            title = (TextView) v.findViewById(R.id.topic);
            imageView = (ImageView) v.findViewById(R.id.image);

            v.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Log.d("RECYCLER", "CLICK");

                }
            });
        }
    }
    public CardAdapter(Context context, Cursor cursor) {
        CardAdapter.context = context;
        dataCursor = cursor;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        cardview = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cardview, parent, false);
        return new ViewHolder(cardview);
    }

    public Cursor swapCursor(Cursor cursor) {
        if (dataCursor == cursor) {
            return null;
        }
        Cursor oldCursor = dataCursor;
        this.dataCursor = cursor;
        if (cursor != null) {
            this.notifyDataSetChanged();
        }
        return oldCursor;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        dataCursor.moveToPosition(position);

      //handle database queries using the dataCursor
    }
    @Override
    public int getItemCount() {
        //replace 8 with 0 when you pass a cursor into the recycler in DemosFragment
        return (dataCursor == null) ? 8 : dataCursor.getCount();
    }
}

设计卡片布局

<强> cardview.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_marginLeft="@dimen/card_margin"
    android:layout_marginRight="@dimen/card_margin"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:clickable="true"
    android:orientation="vertical"
    android:foreground="?attr/selectableItemBackground">
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_weight="2"
            android:layout_height= "0dp">

            <ImageView
                android:id="@+id/image"
                android:scaleType="centerCrop"
                android:src="@drawable/nav_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button"
                android:layout_gravity="right|center_vertical" />

        </FrameLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_marginBottom="@dimen/activity_vertical_margin"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:layout_marginLeft="@dimen/card_text_margin"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/topic"
                    android:text="@string/topic"/>
            </LinearLayout>
    </LinearLayout>

</android.support.v7.widget.CardView>

添加片段/活动

<强> DemosFragment

public class DemosFragment extends Fragment {
    private RecyclerView recyclerView;
    CardAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.Demos_layout, container, false);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view_record);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new CardAdapter(getActivity(), null);
        recyclerView.setAdapter(adapter);
        return rootView;
    }
}

最后不要忘记导入

 compile 'com.android.support:design:23.1.1'

 compile 'com.android.support:cardview-v7:23.1.1'

 compile 'com.android.support:recyclerview-v7:23.1.1'

我的project的完整样本,其概念为