3

在这里,我制作了水平动画列表视图,我在其中使用 list_view.xml 自定义视图

我为制作自定义组件而膨胀它。它可以为每个自定义组件正确分配 id,但我无法通过 getId 方法获取它。

    package com.example.animationdemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements AnimationListener,
        OnClickListener {

    Animation anim;
    LinearLayout myLayout;
    ImageView myImage;

    private static int a = 1000;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myLayout = (LinearLayout) findViewById(R.id.myLayout);

        myImage = (ImageView) findViewById(R.id.imageView1);
        String[] myString = { "sam", "man", "van", "can", "man", "van", "can",
                "man", "van", "can", "man", "van", "can", "man", "van", "can",
                "man", "van", "can", "man", "van", "can", "man", "van", "can",
                "man", "van", "1234" };
        for (String string : myString) {
            View view = new View(this);
            view = inflateEditRow(string);
            view.setOnClickListener(this);
            Log.i("Law", "Id val " + a);
            view.setId(++a);
            myLayout.addView(view);
        }
    }

    private View inflateEditRow(String string) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.list_view, myLayout, false);
        TextView tv = (TextView) row.findViewById(R.id.TextView1);
        tv.setText(string);
        Log.i("Law", "string " + row.getId());
        return row;
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

        /*
         * anim = AnimationUtils .loadAnimation(MainActivity.this,
         * R.anim.animation); anim.setRepeatCount(3); anim.setDuration(30000);
         * myLayout.startAnimation(anim); anim.setAnimationListener(this);
         */
        Log.i("myLayout", "string " + myLayout.getWidth());
        Display display = getWindowManager().getDefaultDisplay();
        @SuppressWarnings("deprecation")
        int stageWidth = display.getWidth();
        Log.i("stageWidth", " " + stageWidth);
        anim = new TranslateAnimation(stageWidth, -28 * dpToPx(200), 0, 0);
        anim.setDuration(2000 * 28);
        // anim.setInterpolator(MainActivity.this,1);
        anim.setFillAfter(true);
        myLayout.startAnimation(anim);
        anim.setAnimationListener(this);
        myLayout.setVisibility(1);
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        myLayout.startAnimation(anim);
        Toast.makeText(getApplicationContext(), "Animation End",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // Toast.makeText(getApplicationContext(), "Animation Repeat",
        // Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onAnimationStart(Animation animation) {
        // Toast.makeText(getApplicationContext(), "Animation Start",
        // Toast.LENGTH_SHORT).show();
    }

    /*
     * private View inflateEditRow(String string) { LayoutInflater inflater =
     * (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View
     * row = inflater.inflate(R.layout.list_view, myLayout, false); TextView tv
     * = (TextView) row.findViewById(R.id.TextView1); tv.setText(string);
     * Log.i("Law", "string " + row.getId()); return row; }
     */

    public int dpToPx(int dp) {
        DisplayMetrics displayMetrics = getBaseContext().getResources()
                .getDisplayMetrics();
        int px = dp
                * Math.round((displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
        Toast.makeText(getApplicationContext(),
                "displayMetrics.xdpi" + displayMetrics.xdpi, Toast.LENGTH_SHORT)
                .show();
        return px;
    }

    @SuppressLint("ShowToast")
    @Override
    public void onClick(View v) {

        Toast.makeText(getApplicationContext(), "Sumant" + v.getId(), 10)
                .show();

    }
}
enter code here

这是我的 activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="50000dp"
    android:layout_height="fill_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:id="@+id/mainLayout" >

   <LinearLayout
        android:id="@+id/myLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

    </LinearLayout>

   <ImageView
       android:id="@+id/imageView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/ic_launcher" />

</LinearLayout>

这是我的 list_View.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TableLayout
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="#FF9900" >

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

            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TableRow
                    android:layout_width="30dp"
                    android:layout_height="40dp" >

                    <ImageView
                        android:id="@+id/imageView1"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:background="#3366FF"
                        android:src="@drawable/ic_launcher" />
                </TableRow>

                <TableRow
                    android:layout_width="30dp"
                    android:layout_height="40dp" >

                    <ImageView
                        android:id="@+id/imageView2"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:background="#FFFF99"
                        android:src="@drawable/ic_launcher" />
                </TableRow>
            </TableLayout>

            <TextView
                android:id="@+id/TextView1"
                android:layout_width="100dp"
                android:layout_height="60dp"
                android:background="#CC33FF"
                android:gravity="center"
                android:padding="10dip"
                android:text="NMDC sdas dsfsdf"
                android:textColor="#ffffff"
                android:textSize="12sp" />

            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TableRow
                    android:layout_width="70dp"
                    android:layout_height="40dp"
                    android:layout_weight="0.5"
                    android:background="#d0000c" >

                    <TextView
                        android:id="@+id/TextView11"
                        android:layout_width="70dp"
                        android:layout_height="30dp"
                        android:gravity="center"
                        android:paddingBottom="8dip"
                        android:paddingTop="8dip"
                        android:text="10000.00"
                        android:textColor="#ffffff"
                        android:textSize="12sp" />
                </TableRow>

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="30dp"
                    android:layout_weight="0.5" >

                    <TableLayout>

                        <TableRow
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1" >

                            <TextView
                                android:id="@+id/TextView21"
                                android:layout_width="30dp"
                                android:layout_height="30dp"
                                android:layout_weight="1"
                                android:background="#d0dc0c"
                                android:gravity="center"
                                android:paddingBottom="8dip"
                                android:paddingTop="8dip"
                                android:text="Row"
                                android:textColor="#ffffff"
                                android:textSize="12sp" />

                            <TextView
                                android:id="@+id/TextView22"
                                android:layout_width="40dp"
                                android:layout_height="30dp"
                                android:layout_weight="1"
                                android:background="#6600FF"
                                android:gravity="center"
                                android:paddingBottom="8dip"
                                android:paddingTop="8dip"
                                android:text="100%"
                                android:textColor="#ffffff"
                                android:textSize="12sp" />
                        </TableRow>
                    </TableLayout>
                </TableRow>
            </TableLayout>
        </TableRow>
    </TableLayout>

</LinearLayout>
4

2 回答 2

1

使用 setTag() 和 getTag() 获取 ID 表单膨胀视图(有用的链接)。

  private View inflateEditRow(String string) {
        LayoutInflater inflater = (LayoutInflater)
                        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.list_view, myLayout, false);
        row = inflater.inflate(R.layout.my_wallet_items, parent, false);
        viewHolder = new MyWrapper(row);
        row.setTag(viewHolder );

        TextView tv = (TextView) row.findViewById(R.id.TextView1);
        tv.setText(string);
        Log.i("Law", "string " + row.getId());
        return row;
    }

for (String string : myString) {
            View view = new View(this); 
            view = inflateEditRow(string);
            viewHolder = (MyWrapper)view.getTag();
            viewHolder.getView.setOnClickListener(this);
          // Log.i("Law", "Id val " + a);
          //  view.setId(++a);
            myLayout.addView(view);
        }

 public class MyWrapper
    {
       private View base;
       private Text txt;

       public MyWrapper(View base)
       {
           this.base = base;
       }
         // others component and getter-setter

     }
于 2013-09-24T09:42:15.470 回答
0

仅当您在 XML 中指定视图时,才会自动生成 ID。如果您以编程方式扩充视图,则可以View.setId(int)在扩充视图后立即使用。

于 2013-09-25T00:19:45.623 回答