简体   繁体   English

如何使用图像/按钮制作大型滚动屏幕?

[英]How to make large scrolling screens with images/buttons?

I have the following code: 我有以下代码:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="false"
    tools:context=".bakers">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
...

in an activity. 在一项活动中。 I have a few images on the activity and know I won't be able to fit them all on one screen, so I added a ScrollView with a height that is much larger than the screen. 我在活动中有几张图像,并且知道无法将它们全部放在一个屏幕上,因此我添加了一个ScrollView,其高度比屏幕大得多。 However, all this does is simply scale the existing images I have so that they are larger and take up more of the screen. 但是,所有这些只是在缩放现有的图像,使它们更大并占据更多屏​​幕。 I've tried fixing the fillViewport and clipToPadding settings, but this doesn't help anything. 我尝试修复fillViewport和clipToPadding设置,但这无济于事。

Essentially what I'm asking is: Is there a way to add an image "below" the preview on screen using a ScrollView, so you can fit more on the screen than you normally would be able to? 本质上,我要问的是:有没有一种方法可以使用ScrollView在屏幕下方“预览”下面添加图像,因此您可以在屏幕上比平时容纳更多的图像? If I make the phone screen larger or the ScrollView larger, the images simply scale up. 如果我将手机屏幕放大或将ScrollView放大,则图像只会按比例放大。

Thanks 谢谢

First you need use an recyclerview RecyclerView documentation 首先,您需要使用一个recyclerview RecyclerView文档

your activity view will have this code 您的活动视图将包含此代码

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <Button
                android:id="@+id/buttonAction"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="button action" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical"
                android:scrollingCache="true" />
        </LinearLayout>

you need create another layout in this case the layout that will display your image, example item_image.xml 在这种情况下,您需要创建另一个布局,该布局将显示您的图像,例如item_image.xml

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/image"
            android:layout_marginTop="4dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:adjustViewBounds="true"
            android:src="@drawable/image_1"/>
    </LinearLayout>

well now you have your views. 好了,现在您有了自己的看法。

the next is create a new class thats implement an adapter: CardAdapter.java 接下来是创建一个实现适配器的新类:CardAdapter.java

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

    Context context;

    public void setContext(Context context) {
        this.context = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imgCover;

        public ViewHolder(final View itemView, int type) {
            super(itemView);
            imgCover = itemView.findViewById(R.id.image);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @NonNull
    @Override
    public CardAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.LayoutParams lp;

        switch (viewType) {
            case 1:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card_payment, null, false);
                lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                view.setLayoutParams(lp);
                break;
        }
        return new CardAdapter.ViewHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    }

    @Override
    public int getItemCount() {
        return 10;
    }

    public Context getContext() {
        return context;
    }
}

and in your activity you will implement the recycler and the adapter 在您的活动中,您将实现回收站和适配器

public class CardsActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    CardAdapter cardAdapter;

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

        initToolbar();
        recyclerView = findViewById(R.id.recyclerView);
        cardAdapter = new CardAdapter();
        cardAdapter.setContext(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(cardAdapter);
    }



}

and its all. 及其全部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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