2

我正在尝试修改此代码以满足我的需求:http ://udinic.wordpress.com/2011/09/03/expanding-listview-items/

我想更改它,以便根据用户的选择显示不同的 TextView。我意识到,使用 if 子句,显然它似乎工作正常。之后我想动态更改 TextViews 的文本,但失败了。setText 仅适用于第一项。第二个总是显示 XML 中设置的静态文本...

谢谢!

列表视图上的侦听器:

       list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {


            ((TextView) findViewById(R.id.title1)).setText("Custom1");
            ((TextView) findViewById(R.id.title2)).setText("Custom2");
            String selectedFromList =(String) (list.getItemAtPosition(position));

            if (selectedFromList.equals("udini1")) {
                View toolbar1 = view.findViewById(R.id.toolbar1);

                // Creating the expand animation for the item
                ExpandAnimation expandAni1 = new ExpandAnimation(toolbar1, 500);

                // Start the animation on the toolbar
                toolbar1.startAnimation(expandAni1);

            }

            if (selectedFromList.equals("udini2")) {
                View toolbar2 = view.findViewById(R.id.toolbar2);

                // Creating the expand animation for the item
                ExpandAnimation expandAni2 = new ExpandAnimation(toolbar2, 500);

                // Start the animation on the toolbar
                toolbar2.startAnimation(expandAni2);


            }



        }
    });
}

list_item.xml

    <LinearLayout android:id="@+id/toolbar1"
      android:layout_marginBottom="-50dip"
      android:visibility="gone"
      android:layout_height="50dip"
      android:layout_width="fill_parent">
     <TextView android:id="@+id/title1"
          android:padding="20dip"
          android:focusable="false"
          android:focusableInTouchMode="false"
          android:layout_width="fill_parent"
          android:text="Dummy1"
          android:layout_height="wrap_content"/>
</LinearLayout>

    <LinearLayout android:id="@+id/toolbar2"
      android:layout_marginBottom="-50dip"
      android:visibility="gone"
      android:layout_height="50dip"
      android:layout_width="fill_parent">
    <TextView android:id="@+id/title2"
          android:padding="20dip"
          android:focusable="false"
          android:focusableInTouchMode="false"
          android:layout_width="fill_parent"
          android:text="Dummy2"
          android:layout_height="wrap_content"/>
</LinearLayout>

自定义列表适配器:

 class CustomListAdapter extends ArrayAdapter<String> {
        public CustomListAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

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

            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);
            }

            ((TextView)convertView.findViewById(R.id.title)).setText(getItem(position));

            // Resets the toolbar to be closed
            View toolbar = convertView.findViewById(R.id.toolbar1);
            ((LinearLayout.LayoutParams) toolbar.getLayoutParams()).bottomMargin = -50;
            toolbar.setVisibility(View.GONE);

            return convertView;
        }
    }

动画类:

public class ExpandAnimation extends Animation {
    private View mAnimatedView;
    private LayoutParams mViewLayoutParams;
    private int mMarginStart, mMarginEnd;
    private boolean mIsVisibleAfter = false;
    private boolean mWasEndedAlready = false;
    /**
     * Initialize the animation
     * @param view The layout we want to animate
     * @param duration The duration of the animation, in ms
     */
    public ExpandAnimation(View view, int duration) {
        setDuration(duration);
        mAnimatedView = view;
        mViewLayoutParams = (LayoutParams) view.getLayoutParams();

        // decide to show or hide the view
        mIsVisibleAfter = (view.getVisibility() == View.VISIBLE);

        mMarginStart = mViewLayoutParams.bottomMargin;
        mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0);

        view.setVisibility(View.VISIBLE);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);

        if (interpolatedTime < 1.0f) {

            // Calculating the new bottom margin, and setting it
            mViewLayoutParams.bottomMargin = mMarginStart
                    + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);

            // Invalidating the layout, making us seeing the changes we made
            mAnimatedView.requestLayout();

        // Making sure we didn't run the ending before (it happens!)
        } else if (!mWasEndedAlready) {
            mViewLayoutParams.bottomMargin = mMarginEnd;
            mAnimatedView.requestLayout();

            if (mIsVisibleAfter) {
                mAnimatedView.setVisibility(View.GONE);
            }
            mWasEndedAlready = true;
        }
    }
}
4

1 回答 1

1

尝试改变

((TextView) findViewById(R.id.title1)).setText("Custom1");
((TextView) findViewById(R.id.title2)).setText("Custom2");

对此

((TextView) view.findViewById(R.id.title1)).setText("Custom1");
((TextView) view.findViewById(R.id.title2)).setText("Custom2");

您当前拥有的行仅获得 ID 为 title1 和 title2 的第一个视图。使用给定view的按 id 查找视图,您将在单击的视图中获得第一个 id 为 title1视图。

于 2013-02-21T12:52:19.930 回答