ListView内嵌HorizontalScrollView

4
我正在尝试在ListView中创建一个水平滚动视图。我已经让ListView工作了,但是将数据放入HorizontalScrollView(HSV)对我来说并不起作用。请给予建议!(我编写了ListView,进行了测试,现在正在尝试添加HorizontalScrollView)
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,但某些地方不像我想的那样工作,请务必帮助!


请查看此链接 https://dev59.com/-m855IYBdhLWcg3wKxHc#4492050。 - Evos
2个回答

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;

来源:Android中的水平ListView?


嘿,谢谢,我会研究一下的。我正在尝试创建一个水平滚动视图列表。这个能用吗? - deep
你的意思是将ListView中的每个项目都作为HSV吗?我不认为以上代码可以实现这个。试试以上代码,然后告诉我你遇到了什么问题... - Shrikant Ballal
是的,那正是我想要做的。可以看看 Android 上的 Pulse 新闻应用程序,那就类似于我想要实现的功能。 - deep

0
在你的代码中,这一行不正确,你正在创建一个新的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相关的部分不起作用。我认为我没有正确地应用它。你能给予建议吗? - deep
好的,也许这不是因为你按照我的指示操作所导致的问题。但是可能是由于您的listView获取了触摸事件,因此您的HSV无法获取它。您可能需要处理触摸事件。 - yahya
我的HSV布局中只有一个TextView,但是当HSV被填充时,每个HSV将会有多个TextView。 - deep

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接