Content of a slideable layout

0

I have a layout with a lot of buttons. My idea is that although they do not all show at the same time, if I can move from top to bottom the content of that layout to be able to navigate in it. But I do not know how I can do it ... Any idea? attached image and code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/background">

<LinearLayout
    android:id="@+id/saldoLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/saldoTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/saldo"
        android:textColor="@color/letra"
        android:textSize="24sp"
        android:textAlignment="center"/>
</LinearLayout>

<LinearLayout
    android:id="@+id/topLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="125dp"
    android:layout_below="@+id/saldoLinearLayout"
    android:orientation="horizontal">

</LinearLayout>

<LinearLayout
    android:id="@+id/midTableLayout"
    android:layout_width="match_parent"
    android:layout_height="355dp"
    android:layout_below="@+id/topLinearLayout"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/topLinearLayout"
        android:layout_centerHorizontal="true"
        android:text="Button" />

    <Button
        android:id="@+id/button10"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button"
        android:layout_below="@+id/button"
        android:text="Button" />

    <Button
        android:id="@+id/button13"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button4"
        android:layout_below="@+id/button4"
        android:text="Button" />

    <Button
        android:id="@+id/button11"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button2"
        android:layout_below="@+id/button2"
        android:text="Button" />

    <Button
        android:id="@+id/button6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button5"
        android:layout_below="@+id/button5"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button3"
        android:layout_below="@+id/button3"
        android:text="Button" />

    <Button
        android:id="@+id/button8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

<LinearLayout
    android:id="@+id/menuLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/midTableLayout"
    android:orientation="vertical">

    <Button
        android:id="@+id/homeButton"
        style="@style/Widget.AppCompat.Button"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Home" />

    <Button
        android:id="@+id/noticiasButton"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Noticias" />

    <Button
        android:id="@+id/estadoButton"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Estado" />
</LinearLayout>

</RelativeLayout>
    
asked by Eduardo 21.04.2017 в 19:59
source

1 answer

1

You can create a RecyclerView if you have a loaded view within what You want to do Scroll.

You can also do a Listview instead of creating as many buttons:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/background">

<LinearLayout
    android:id="@+id/saldoLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/saldoTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/saldo"
        android:textColor="@color/letra"
        android:textSize="24sp"
        android:textAlignment="center"/>
</LinearLayout>

<LinearLayout
    android:id="@+id/topLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="125dp"
    android:layout_below="@+id/saldoLinearLayout"
    android:orientation="horizontal">

</LinearLayout>

<LinearLayout
    android:id="@+id/midTableLayout"
    android:layout_width="match_parent"
    android:layout_height="355dp"
    android:layout_below="@+id/topLinearLayout"
    android:orientation="vertical">

    <ListView
        android:id="@+id/lvListaDeBotones"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

</LinearLayout>

<LinearLayout
    android:id="@+id/menuLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/midTableLayout"
    android:orientation="vertical">

    <Button
        android:id="@+id/homeButton"
        style="@style/Widget.AppCompat.Button"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Home" />

    <Button
        android:id="@+id/noticiasButton"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Noticias" />

    <Button
        android:id="@+id/estadoButton"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Estado" />
</LinearLayout>

</RelativeLayout>

Create this in your activity:

static final String[] TITULOS_BTN =
            new String[] { "Button 1", "Button 2", "Button 3", "Button 4", "Button 5"};

public ListView lvListaDeBotones;

Add this code in the onCreate or where you want to initialize your listview associated with the parent view:

lvListaDeBotones = (ListView)findViewById(R.id.lvListaDeBotones);
MyAdapter mAdapter = new MyAdapter(this, TITULOS_BTN);
lvListaDeBotones.setAdapter(mAdapter);

lvListaDeBotones.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Aquí puedes capturar en on Click de la vista que se crea en general.
            }
        });

Create the view for the items in your lisview :

listview_items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnAction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

</LinearLayout>

Create your adapter, which will contain the child view of your listview :

Note: do not forget to add the associated package :

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;

public class MyAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MyAdapter(Context context, String[] values) {
        super(context, R.layout.listview_items, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.listview_items, parent, false);
        Button btnAction = (Button) rowView.findViewById(R.id.btnAction);
        btnAction.setText(values[position]);

        return rowView;
    }
}

To create more buttons, just add titles to your String [] , greetings.

If your view is very large and some buttons are not visible, after adding the lsitview, use as parent a <ScrollView> </ScrollView> above the first <RelativeLayout> and if you are working with API Level > 21, use this property in <ListView> android:nestedScrollingEnabled="true" to be able to scroll independently.

    
answered by 21.04.2017 / 22:52
source