【问题标题】:HorizontalScrollView inside ListViewListView 内的 Horizo​​ntalScrollView
【发布时间】:2012-12-04 14:04:01
【问题描述】:

我正在尝试在 ListView 中制作水平滚动视图。我有 ListView 工作,但将数据放入 Horizo​​ntalscrollview(HSV) 对我来说不起作用。请指教! (我对列表视图进行了编码,经过测试,现在尝试添加一个水平滚动视图)

HSV 将适用于每个列表项。

所以基本上对于我如何处理这个问题的逻辑:我有我的 listview 适配器,我决定将 HSV 放在适配器内,以便它循环遍历每个 listItem 并在其中放置一个 HSV。

我的 Xml 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:baselineAligned="false"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout
    android:orientation="vertical"
    android:layout_width="0dip" android:layout_weight="1"
    android:layout_height="fill_parent">


        <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtProjectName" />

        <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtProjectDescription" />

        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >

            <LinearLayout 
                android:id="@+id/projectTasks"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/txtProjectTasks" />

            </LinearLayout>

        </HorizontalScrollView>
    </LinearLayout>
</LinearLayout>

然后我创建了一个自定义适配器来运行所有项目。

public class ProjectListAdapter extends ArrayAdapter<Projects> {

int resource;
String response;
Context context;
ArrayList<Tasks> taskArray = null;

// Initialize adapter
public ProjectListAdapter(Context context, int resource,
        List<Projects> items) {
    super(context, resource, items);
    this.resource = resource;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout projectView;

    // Get the current project object
    Projects project = getItem(position);
    //
    // Inflate the view
    if (convertView == null) {
        projectView = new LinearLayout(getContext());
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi;
        vi = (LayoutInflater) getContext().getSystemService(inflater);
        vi.inflate(resource, projectView, true);
    } else {
        projectView = (LinearLayout) convertView;
    }

    TextView PROJECT_NAME = (TextView) projectView
            .findViewById(R.id.txtProjectName);
    TextView PROJECT_DESCRIPTION = (TextView) projectView
            .findViewById(R.id.txtProjectDescription);

    PROJECT_NAME.setText(project.getNAME());
    PROJECT_DESCRIPTION.setText(project.getDESCRIPTION());
    taskArray = new ArrayList<Tasks>();
    taskArray = (ArrayList<Tasks>) project.getTasks();

    for (Tasks tasks : taskArray) {
        HorizontalScrollView TASKS = (HorizontalScrollView) projectView
                .findViewById(R.id.projectTasks);
        LinearLayout taskLayout = (LinearLayout) projectView
                .findViewById(R.id.projectTasks);
        TextView taskTxt = (TextView) projectView
                .findViewById(R.id.txtProjectTasks);
        taskTxt.setText(tasks.getTASK_ID());
        taskLayout.addView(taskTxt);

    }
    return projectView;
}

}

最后一段代码是适配器循环生成 HSV 的地方,但有些东西不能按我的意愿工作,请帮忙!

【问题讨论】:

标签: android


【解决方案1】:

请按照以下链接在 ListView 中使用 HSV: http://www.dev-smart.com/archives/34

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean handled = mGesture.onTouchEvent(event);
    return handled;
}
Then, add the following code which will decide to steal the event from the item children and give it to our onTouchEvent, or let it be handled by them.

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch( ev.getActionMasked() ){
        case MotionEvent.ACTION_DOWN:
             mInitialX = ev.getX();
             mInitialY = ev.getY();             
             return false;
        case MotionEvent.ACTION_MOVE:
             float deltaX = Math.abs(ev.getX() - mInitialX);
             float deltaY = Math.abs(ev.getY() - mInitialY);
             return ( deltaX > 5 || deltaY > 5 );
        default:
             return super.onInterceptTouchEvent(ev);
    }
}
Finally, don't forget to declare the variables in your class:

private float mInitialX;
private float mInitialY;

来源:Horizontal ListView in Android?

【讨论】:

  • 嘿,谢谢,我看看这个。我正在尝试拥有一个水平滚动视图列表。这有可能吗?
  • 你的意思是说 ListView 中的每个项目都是 HSV?我认为上面的代码不可能。试试上面的代码,让我知道你遇到了什么问题......
  • 是的,这正是我想要做的。查看适用于 android 的脉冲新闻应用程序。这与我想要实现的目标相似。
【解决方案2】:

在您的代码中,此行不正确,您正在创建一个新的 linearLayout 但它不是来自您的自定义视图:

projectView = new LinearLayout(getContext());

所以试试这个:

View projectView = convertView;

// Inflate the view
if (projectView == null) {
    String inflater = Context.LAYOUT_INFLATER_SERVICE;
    LayoutInflater vi;
    vi = (LayoutInflater) getContext().getSystemService(inflater);
    projectView = vi.inflate(resource, false);
} 

并将您的 projectView 定义为 View,而不是 LinearLayout。

【讨论】:

  • 我进行了这些更改,并在没有 HSV 的情况下对其进行了测试,并且代码确实有效,但是 HSV 的东西不起作用。我相信我没有正确充气。你能建议吗?
  • 好吧,如果你按照我说的做,这可能与膨胀无关……但我可能是你的 listView 获取触摸事件,所以你的 HSV 无法得到它。您可能需要处理触摸事件。
  • 我的 HSV 在其布局中只有一个 TextView,但是当 HSV 被填充时,每个 HSV 将有多个 TextView。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多