سؤال

I needed a Horizontal ListView and was advised to use TableRow instead. Now, I have problem. The TableRow simply won't scroll horizontally. How to solve this issue?

هل كانت مفيدة؟

المحلول

Simply wrap the TableView into a HorizontalScrollView. Something like this:

  <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:scrollbars="horizontal">

    <TableView
        android:id="@+id/your_tv"
        ...>
    </TableView>
 </HorizontalScrollView>

---- EDIT ----

To know which View have you clicked, as you know you have the tag and that when you fire an onClick() event you'll get the View that fired it by parameter, you can use something like this:

your_item.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    Log.d("TagDebug", "Hey, I'm the child number " + v.getTag().toString());
  }
});

نصائح أخرى

Share some of your code so we can at least know what you tried.

AFAIK if you put TableRow in a ScrollView, it works. This is what I did while working on simple table;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="false" >

    <TableLayout
        android:id="@+id/table"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:dividerPadding="@dimen/activity_horizontal_margin"
        android:orientation="horizontal"
        android:showDividers="beginning|middle|end"
        android:stretchColumns="0,1,2,3" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" >

            <TextView
                android:id="@+id/p1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/p1"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/p2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/p2"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/p3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/p3"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/p4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/p4"
                android:textStyle="bold" />

        </TableRow>
    </TableLayout>
</ScrollView>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" >

    <EditText
        android:id="@+id/newScore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="numberSigned" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/newScore2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="numberSigned" />

    <EditText
        android:id="@+id/newScore3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="numberSigned" />

    <EditText
        android:id="@+id/newScore4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="numberSigned" />

    <Button
        android:id="@+id/addScore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/addScore" />
</LinearLayout>

And you can add row to table like this;

tl = (TableLayout) findViewById(R.id.table);    
TableRow row = new TableRow(this);
        TextView textView = new TextView(this);
        TextView textView2 = new TextView(this);
        TextView textView3 = new TextView(this);
        TextView textView4 = new TextView(this);
        textView.setText(score + "");
        textView2.setText(score2 + "");
        textView3.setText(score3 + "");
        textView4.setText(score4 + "");
        row.addView(textView);
        row.addView(textView2);
        row.addView(textView3);
        row.addView(textView4);
        tl.addView(row);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top