禁用列表视图滚动并启用整个布局 [英] DIsable scrolling for listview and enable for whole layout

查看:14
本文介绍了禁用列表视图滚动并启用整个布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 android 应用程序,它在主活动中有两个列表视图.我想要的是禁用两个列表的滚动并只允许整个页面滚动,有什么方法可以帮助.....我的代码包 com.example.listviewdemo;

Hi iam currently working on an android application it has two list views inside the main activity.What i want is disable the scrolling of two lists and allow the whole page to scroll only,is there any way for that please do help..... my code package com.example.listviewdemo;

 import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import android.widget.Toast;

public class MainActivity extends Activity {

ListView list,list2;
String[] name={"Happy","always","try","hard","you will","get it!","Believe","in","God","everything","will","work well!","Believe","in","God","everything","will","work well!"};
String[] name2={"Believe","in","God","everything","will","work well!","Believe","in","God","everything","will","work well!"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list = (ListView) findViewById(R.id.listview);
    list2 = (ListView) findViewById(R.id.listview2);
    list.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,name));
  list2.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,name2));



    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), name [position],Toast.LENGTH_SHORT).show();
        }
    });

list2.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), name2 [position],Toast.LENGTH_SHORT).show();
        }
    });   
}   
  }

我的xml代码是:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
     android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:orientation="vertical"
     tools:context="${relativePackage}.${activityClass}" >
    <TextView
    android:id="@+id/text_id1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
  android:text="@string/str1" />
   <ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
     android:layout_height="wrap_content"
    />
    <TextView
      android:id="@+id/text_id2"
     android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/str2" 

     />

      <ListView
    android:id="@+id/listview2"
    android:layout_width="match_parent"
     android:layout_height="wrap_content"
       />
      </LinearLayout>

推荐答案

你可以试试这个.

对于 xml 部分这样做:

FOR xml PART DO THIS:

将整个布局数据放在一个滚动视图下,例如:

Put your entire layout data under one Scroll View, for example:

    <ScrollView
        android:id="@+id/scrollViewId"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >             

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

                <ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" /> // SAY YOUR FIRST LIST VIEW:

                <ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" /> // SAY YOUR SECONDLIST VIEW:

                 // Add your other views as per requirement....

        </LinearLayout>

    </ScrollView>

现在在Java课上做以下事情...

NOW IN JAVA CLASS DO THE FOLLOWING THING...

将适配器设置为列表视图后,只需将此自定义方法添加到您的代码中即可:

Just add this custom method to your code after setting adapter to list view:

setListViewHeightBasedOnChildren(listview)

例如:

      list = (ListView) findViewById(R.id.listview);
      list.setAdapter(new ArrayAdapter<String> 
                      (MainActivity.this,android.R.layout.simple_list_item_1,name));
      setListViewHeightBasedOnChildren(list);

对第二个列表视图也这样做.

Do it same for second list view too.

这里是 setListViewHeightBasedOnChildren 方法的主体

Here is body of setListViewHeightBasedOnChildren METHOD

   public static void setListViewHeightBasedOnChildren(ListView listView) 
{
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight=0;
    View view = null;

    for (int i = 0; i < listAdapter.getCount(); i++) 
    {
        view = listAdapter.getView(i, view, listView);

        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,  
                                      LayoutParams.MATCH_PARENT));

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();

    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));

    listView.setLayoutParams(params);
    listView.requestLayout();

}

希望它对你有用.

这篇关于禁用列表视图滚动并启用整个布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆