0

Здравствуйте возникла проблема есть маинактивити с дизайном из 3 плит линеарлайоута примерно так пример вот код этой плитки:

<LinearLayout 
         android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
   android:gravity="center" 
        android:background="#092E47"
        android:layout_marginLeft="2dp"
         android:layout_marginRight="2dp"
        >
             <LinearLayout 
         android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
   android:gravity="center"     
        >
             <ImageView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_margin="5dp"

             android:src="@drawable/contact" />
             </LinearLayout>
             <LinearLayout 
         android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
   android:gravity="center" 
   android:orientation="vertical"

        >
              <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="заголовок"
            android:textColor="#D0DFD7"
            android:textSize="18sp"

            android:typeface="normal" />
         <TextView
             android:layout_marginTop="3dp"
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Новости"
            android:textColor="#D0DFD7"
            android:textSize="16sp"

            android:typeface="normal" />
         </LinearLayout>
     </LinearLayout>

мне нужно сделать свайп новостей в бок

Мне нужен именно свайп только на этом линеарлайоут

1 ответ 1

1

ScrollView в помощь. Во второй LinearLayout вставь

<ScrollView
   android:id="@+id/scroll"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >
   <LinearLayout
        android:id="@+id/llnews"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
   </LinearLayout>
</ScrollView>

создать еще один Layout для элемента: (listitem.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tvNewsHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="22dp"
        android:textStyle="bold"
        android:textColor="#D0DFD7" />
    <TextView
        android:id="@+id/tvNewsBody"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" 
        android:textColor="#D0DFD7"/>
</LinearLayout>

и затем с помощью LayoutInflater заполнить список: (MainActivity.java)

    public class MainActivity extends Activity {
      String[] newhead = { "Заголовок 1", "Заголовок 2", 
                  "Заголовок 3", "Заголовок 4", 
                  "Заголовок 5", "Заголовок 6",
                  "Заголовок 7", "Заголовок 8" }; 
          String[] newsbody = { "Представители компании Google заявили 1",
 "Представители компании Google заявили 2",
 "Представители компании Google заявили 3",
 "Представители компании Google заявили 4",
 "Представители компании Google заявили 5", 
 "Представители компании Google заявили 6", 
 "Представители компании Google заявили 7", 
 "Представители компании Google заявили 8" };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout linLayout = (LinearLayout) findViewById(R.id.llnews);  
        LayoutInflater llInflater = getLayoutInflater();

        for (int i = 0; i < newhead.length; i++) {

            View item = llInflater.inflate(R.layout.listitem, linLayout, false);
            TextView tvNewsHeader = (TextView) item.findViewById(R.id.tvNewsHeader);
            tvNewsHeader.setText(newhead[i]);
            TextView tvNewsBody = (TextView) item.findViewById(R.id.tvNewsBody);
            tvNewsBody.setText(newsbody[i]);
            item.getLayoutParams().width = LayoutParams.MATCH_PARENT;
            linLayout.addView(item);
          }     
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

Как-то так. Но на мой взгляд, лучше использовать ListView.

УПС! Протормозил. Но если во второй LinearLayout вставить ViewPager, то можно сделать. Я попробовал вставить пример из статьи Android Tab Layout with Swipeable Views и все получилось. только нужно спрятать Actionbar: actionBar.hide();

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.