简体   繁体   中英

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

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? If I make the phone screen larger or the ScrollView larger, the images simply scale up.

Thanks

First you need use an recyclerview RecyclerView documentation

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

   <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

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.

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