我在此布局中有多个线性布局。滚动视图在我完全滚动项目时工作,但不在列表视图中。我的列表视图有多个列表项目,它一次只显示一个列表项目谢谢帮助我.....
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/taskscrollviewid"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/txtdate"
android:layout_width="0dip"
android:layout_height="30px"
android:layout_weight="1"
android:background="@drawable/taskbuttonselector" />
<Button
android:id="@+id/btnTaskTimeid"
android:layout_width="0dip"
android:layout_height="30px"
android:layout_weight="1"
android:background="@drawable/taskbuttonselector" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/txtItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittextselector"
android:drawableEnd="@drawable/inbox"
android:gravity="top|left"
android:hint="Enter task"
android:inputType="textMultiLine"
android:maxLines="5"
android:minLines="3"
android:scrollbars="vertical"
android:singleLine="false" />
<ImageButton
android:id="@+id/imagebuttonid"
android:layout_width="31dp"
android:layout_height="36dp"
android:layout_gravity="right|bottom"
android:background="@drawable/inbox" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/btnaddcategoryid"
android:layout_width="0dip"
android:layout_height="30px"
android:layout_weight="1"
android:background="@drawable/taskbuttonselector"
android:text="AddCategory" />
<Button
android:id="@+id/btnaddseverityid"
android:layout_width="0dip"
android:layout_height="30px"
android:layout_weight="1"
android:background="@drawable/taskbuttonselector"
android:text="AddSeverity" />
</LinearLayout>
<Button
android:id="@+id/btnadddb"
android:layout_width="wrap_content"
android:layout_height="30px"
android:layout_gravity="center|center_horizontal"
android:layout_marginTop="10dp"
android:background="@drawable/taskbuttonselector"
android:text="Add This Task" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/fillbut"
android:layout_width="0dip"
android:layout_height="30px"
android:layout_weight="1"
android:background="@drawable/taskbuttonselector"
android:text="Remain Task" />
<Button
android:id="@+id/remainbut"
android:layout_width="0dip"
android:layout_height="30px"
android:layout_weight="1"
android:background="@drawable/taskbuttonselector"
android:text="Finish Task" />
</LinearLayout>
<ListView
android:id="@+id/listid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:clickable="true"
android:orientation="vertical" >
</ListView>
</LinearLayout>
</ScrollView>
答案 0 :(得分:3)
永远不要在滚动视图中放置列表视图,反之亦然!它在documentation:
中说得很对你永远不应该使用带有ListView的ScrollView,因为ListView负责自己的垂直滚动。最重要的是,这样做会使ListView中的所有重要优化都无法处理大型列表,因为它有效地强制ListView显示其整个项目列表以填充ScrollView提供的无限容器。
答案 1 :(得分:1)
ListView本身是可滚动的。
您不应该将ListView放在ScrollView中,因为ListView类实现了自己的滚动,它只是不接收手势,因为它们都由父ScrollView处理。
http://developer.android.com/reference/android/widget/ScrollView.html
您可以删除滚动视图或在滚动视图外移动列表视图。
您可以查看此链接。谈话在listview上,他们建议不要将listview放在scrollview中。
答案 2 :(得分:0)
创建类像这样:
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
在您想要的位置调用此方法滚动您的列表视图。
Utility.setListViewHeightBasedOnChildren(YourListView);
适用于您的Reference。
希望这会对你有所帮助。
答案 3 :(得分:0)
//requestDisallowInterceptTouchEvent(true);
// use above method to disable listview scroll and enabled scrollview scroll
// to disable listview.requestDisallowInterceptTouchEvent(true);
// to enabled scrollview scroll write as below
// ex: ((MainActivity) context).listview.requestDisallowInterceptTouchEvent(true);
// i have used above things on viewHolder.l1.setOnTouchListener(new View.OnTouchListener()
=============================
Main Activity
=============================
package com.example.scrollview_demo;
import java.util.ArrayList;
import com.example.scrollview_demo.Scroll_adapter.ViewHolder;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends Activity {
public ListView listview;
ArrayList<String>data;
Scroll_adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView)findViewById(R.id.listView1);
data=new ArrayList<String>();
data.add("1");
data.add("1");
data.add("1");
data.add("1");
data.add("1");
data.add("1");
data.add("1");
data.add("1");
data.add("1");
data.add("1");
data.add("1");
adapter=new Scroll_adapter(MainActivity.this,data);
listview.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void setListfalse() {
listview.requestDisallowInterceptTouchEvent(false);
}
}
Scroll Adapter
package com.example.scrollview_demo;
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class Scroll_adapter extends ArrayAdapter<String>
{
private final Activity context;
ArrayList<String>data;
class ViewHolder
{
public TextView tv_name;
public ScrollView scroll;
public LinearLayout l1;
}
public Scroll_adapter(Activity context, ArrayList<String> all_data) {
super(context, R.layout.item_list, all_data);
this.context = context;
//data=all_data;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder viewHolder;
if (rowView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.item_list, null);
viewHolder = new ViewHolder();
viewHolder.scroll=(ScrollView)rowView.findViewById(R.id.scrollView1);
viewHolder.l1=(LinearLayout)rowView.findViewById(R.id.layout);
viewHolder.l1.setVerticalScrollBarEnabled(true);
rowView.setTag(viewHolder);
}
else
viewHolder = (ViewHolder) rowView.getTag();
viewHolder.l1.removeAllViews();
// create dynamic Linear layout and add textview or whatever u need just add into dynamic created layout
for(int i=0;i<10;i++)
{
LinearLayout LL = new LinearLayout(context);
LL.setOrientation(LinearLayout.VERTICAL);
LL.setPadding(0,5,0,0);
final TextView t1=new TextView(context);
//t1.setText("hello");
t1.setText(""+ (i+1));
LL.addView(t1);
viewHolder.l1.addView(LL);
}
viewHolder.l1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
// stop listview scrolling and enable scrollview inside listview item
((MainActivity) context).listview.requestDisallowInterceptTouchEvent(true);
return false;
}
});
return rowView;
}
}