如何在android cardviews的顶部添加图标(如搜索、心形、选项),如图片?

2q5ifsrm  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(338)

我已经编写了一个小应用程序,它显示了几个android卡,每个卡都有彩色的标题。
我希望能够添加图标到每个卡的右上角,像在下面的图片到目前为止,虽然我已经发现了下面这样的屏幕截图表明这是可能的。我还没有在网上找到任何关于如何做到这一点的信息。所以一些帮助将是美妙的:-)

我的代码如下:
cardadapter.java文件

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

    public List<TTItem> posts = new ArrayList<>();

    public void addItems(List<TTItem> items) {
        posts.addAll(items);
        notifyDataSetChanged();
    }

    public void clear() {
        posts.clear();
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = View.inflate(context, R.layout.item_cardview, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTextView.setText(posts.get(position).title);
        Picasso.with(holder.mImageView.getContext()).load(posts.get(position).images[0]).into(holder.mImageView);
    }

    @Override
    public int getItemCount() {
        return posts.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ImageView mImageView;

        public ViewHolder(View view) {
            super(view);
            mTextView = (TextView) view.findViewById(R.id.textview);
            mImageView = (ImageView) view.findViewById(R.id.imageView);
        }
    }
}

主活动.java

public class MainActivity extends ActionBarActivity implements SwipeRefreshLayout.OnRefreshListener {
    @InjectView(R.id.mainView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.refreshContainer)
    SwipeRefreshLayout refreshLayout;
    private LinearLayoutManager mLayoutManager;
    private CardAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        mRecyclerView.setHasFixedSize(true);
        refreshLayout.setOnRefreshListener(this);
        TypedValue tv = new TypedValue();
        int actionBarHeight = 0;
        if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
        }
        refreshLayout.setProgressViewEndTarget(true, actionBarHeight);
        mAdapter = new CardAdapter();
        mRecyclerView.setAdapter(mAdapter);
        // use a linear layout manager
        mLayoutManager = new GridLayoutManager(this, 1);
        mRecyclerView.setLayoutManager(mLayoutManager);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onRefresh() {
        mAdapter.clear();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                refreshLayout.setRefreshing(false);
            }
        }, 2500);
    }
}

活动\u main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refreshContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/mainView"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

项目\u cardview.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    app:cardElevation="8dp"
    card_view:cardCornerRadius="2dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout     <!-- This is the specific part you asked to color -->
            android:id="@+id/heading_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/teal_500"
            android:padding="36dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_heading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="22 mins to Ancona"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:textSize="36sp" />

            <TextView
                android:id="@+id/tv_subheading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:layout_below="@+id/tv_heading"
                android:text="Light traffic on ss16"
                android:textColor="@color/teal_200"
                android:textSize="24sp" />

        </LinearLayout>

        <ImageView
            android:id="@+id/iv_map"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="Assigned delivery boy"
            android:scaleType="fitXY"
            android:src="@drawable/bg_map" />

        <TextView
            android:id="@+id/tv_footer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:layout_below="@+id/tv_heading"
            android:text="It is just an example!"
            android:textColor="@color/grey_500"
            android:textStyle="bold"
            android:textSize="24sp" />

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

kr98yfug1#

所以你想在卡片上添加图标,而不是在活动的工具栏上,对吧?然后在卡片顶部添加一个相对位置。您可以将图标添加到此布局中,并让它们向右对齐。卡片的主要内容应在相对布局下方对齐。瞧:)

相关问题