0

我想用左边的 ImageView 和右边的文本来实现 ListView 动画,我正在使用一个适配器来为 ListView 提供动画,它们很棒,所以我想知道我该怎么做修改类,特别是适配器以在左侧显示带有 ImageView 的 ListView。这是课程:

public class Post extends SherlockActivity implements
        PullToRefreshAttacher.OnRefreshListener, OnItemLongClickListener,
        OnItemClickListener {

    private PullToRefreshAttacher mPullToRefreshAttacher;
    private ListView listview;
    private DisplayMetrics metrics;
    ActionMode mMode;
    Animation animation;
    Intent i;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        sessionCheck();
    }

    private void sessionCheck() {
        // TODO Auto-generated method stub
        Session session = Session.getActiveSession();
        if (session != null && session.isOpened()) {
            // Get the user's data
            listview = new ListView(this);
            listview.setFadingEdgeLength(0);
            ArrayList<String> strings = new ArrayList<String>();

            for (int i = 0; i < 100; i++) {
                strings.add("That's list number " + (i + 1));
            }

            MainAdapter mAdapter = new MainAdapter(this, strings, metrics);

            listview.setAdapter(mAdapter);
            listview.setOnItemLongClickListener(this);
            listview.setOnItemClickListener(this);
            setContentView(listview);
            pullToRefresh();
        } else {
            Toast.makeText(getApplicationContext(), "Please log in first",
                    Toast.LENGTH_LONG).show();
            i = new Intent(getApplicationContext(), StartBase.class);
            startActivity(i);
        }
    }

    private void pullToRefresh() {
        // TODO Auto-generated method stub
        PullToRefreshAttacher.Options ptrOptions = new PullToRefreshAttacher.Options();
        ptrOptions.refreshScrollDistance = 0.6f;
        ptrOptions.headerLayout = R.layout.customised_header;
        ptrOptions.headerTransformer = new CustomisedHeaderTransformer();
        mPullToRefreshAttacher = PullToRefreshAttacher.get(this, ptrOptions);
        PullToRefreshAttacher.ViewDelegate handler = new AbsListViewDelegate();
        mPullToRefreshAttacher.addRefreshableView(listview, handler, this);
    }

    @Override
    public void onRefreshStarted(View view) {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);

                // Notify PullToRefreshAttacher that the refresh has finished
                mPullToRefreshAttacher.setRefreshComplete();
            }
        }.execute();
    }

    static class CustomisedHeaderTransformer extends
            PullToRefreshAttacher.HeaderTransformer {

        private View mHeaderView;
        private TextView mMainTextView;
        private TextView mProgressTextView;

        @Override
        public void onViewCreated(Activity activity, View headerView) {
            mHeaderView = headerView;
            mMainTextView = (TextView) headerView.findViewById(R.id.ptr_text);
            mProgressTextView = (TextView) headerView
                    .findViewById(R.id.ptr_text_secondary);
        }

        @Override
        public void onReset() {
            mMainTextView.setVisibility(View.VISIBLE);
            mMainTextView.setText(R.string.pull_to_refresh_pull_label);

            mProgressTextView.setVisibility(View.GONE);
            mProgressTextView.setText("");
        }

        @Override
        public void onPulled(float percentagePulled) {
            mProgressTextView.setVisibility(View.VISIBLE);
            mProgressTextView
                    .setText(Math.round(100f * percentagePulled) + "%");
        }

        @Override
        public void onRefreshStarted() {
            mMainTextView.setText(R.string.pull_to_refresh_refreshing_label);
            mProgressTextView.setVisibility(View.GONE);
        }

        @Override
        public void onReleaseToRefresh() {
            mMainTextView.setText(R.string.pull_to_refresh_release_label);
        }

        @Override
        public void onRefreshMinimized() {
            // In this header transformer, we will ignore this call
        }

        @Override
        public boolean showHeaderView() {
            final boolean changeVis = mHeaderView.getVisibility() != View.VISIBLE;
            if (changeVis) {
                mHeaderView.setVisibility(View.VISIBLE);
            }
            return changeVis;
        }

        @Override
        public boolean hideHeaderView() {
            final boolean changeVis = mHeaderView.getVisibility() != View.GONE;
            if (changeVis) {
                mHeaderView.setVisibility(View.GONE);
            }
            return changeVis;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.main, menu);

        SubMenu subMenu1 = menu.addSubMenu("Categories");
        subMenu1.add(0, 1, Menu.NONE, "One");
        subMenu1.add(0, 2, Menu.NONE, "Two");
        subMenu1.add(0, 3, Menu.NONE, "Three");
        subMenu1.add(0, 4, Menu.NONE, "Four");
        subMenu1.add(0, 5, Menu.NONE, "Five");
        subMenu1.add(0, 6, Menu.NONE, "Six");

        MenuItem subMenu1Item = subMenu1.getItem();
        subMenu1Item.setIcon(android.R.drawable.ic_dialog_info);
        subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
                | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case 1:
                Toast.makeText(getApplicationContext(), "That's selected",
                        Toast.LENGTH_LONG).show();
                break;
            case 2:
                Toast.makeText(getApplicationContext(), "That's selected",
                        Toast.LENGTH_LONG).show();
                break;
            case 3:
                Toast.makeText(getApplicationContext(), "That's selected",
                        Toast.LENGTH_LONG).show();
                break;
            case 4:
                Toast.makeText(getApplicationContext(), "That's selected",
                        Toast.LENGTH_LONG).show();
                break;
            case 5:
                Toast.makeText(getApplicationContext(), "That's selected",
                        Toast.LENGTH_LONG).show();
                break;
            case 6:
                Toast.makeText(getApplicationContext(), "That's selected",
                        Toast.LENGTH_LONG).show();
                break;
            case R.id.action_account:
                i = new Intent(getApplicationContext(), Account.class);
                startActivity(i);
                break;
            case R.id.action_logout:
                Toast.makeText(getApplicationContext(), "Working on this...",
                        Toast.LENGTH_LONG).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public class MainAdapter extends ArrayAdapter<String> {
        private Context context;
        private LayoutInflater mInflater;
        private ArrayList<String> strings;
        private DisplayMetrics metrics_;

        private class Holder {
            public TextView textview;
        }

        public MainAdapter(Context context, ArrayList<String> strings,
                           DisplayMetrics metrics) {
            super(context, 0, strings);
            this.context = context;
            this.mInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.strings = strings;
            this.metrics_ = metrics;
        }

        @Override
        public View getView(final int position, View convertView,
                            ViewGroup parent) {
            final String str = this.strings.get(position);
            final Holder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(
                        android.R.layout.simple_list_item_1, null);
                convertView.setBackgroundColor(0xFF202020);

                holder = new Holder();
                holder.textview = (TextView) convertView
                        .findViewById(android.R.id.text1);
                holder.textview.setTextColor(0xFFFFFFFF);
                holder.textview.setBackgroundResource(R.drawable.list_selector);

                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }

            holder.textview.setText(str);

            animation = animation = AnimationUtils.loadAnimation(context,
                    R.anim.push_left_in);

            animation.setDuration(200);
            convertView.startAnimation(animation);
            animation = null;

            return convertView;
        }

    }

    private final class AnActionModeOfEpicProportions implements
            ActionMode.Callback {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {

            menu.add("One").setIcon(android.R.drawable.ic_dialog_info)
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

            menu.add("Two").setIcon(android.R.drawable.ic_dialog_info)
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

            menu.add("Three").setIcon(android.R.drawable.ic_dialog_info)
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

            menu.add("Four").setIcon(android.R.drawable.ic_dialog_info)
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

            menu.add("Five").setIcon(android.R.drawable.ic_dialog_info)
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

            menu.add("Six").setIcon(android.R.drawable.ic_dialog_info)
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            Toast.makeText(getApplicationContext(), "Got click: " + item,
                    Toast.LENGTH_SHORT).show();
            mode.finish();
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
                                   long arg3) {
        // TODO Auto-generated method stub
        mMode = startActionMode(new AnActionModeOfEpicProportions());
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "List item selected",
                Toast.LENGTH_LONG).show();
    }

}

我刚刚复制粘贴了所有内容 :-) 您正在寻找的适配器是 MainAdapter...在那儿。请帮帮我。

4

1 回答 1

0

您应该修改这部分:

if (convertView == null) {
    convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
    convertView.setBackgroundColor(0xFF202020);

    holder = new Holder();
    holder.textview = (TextView) convertView.findViewById(android.R.id.text1);
    holder.textview.setTextColor(0xFFFFFFFF);
    holder.textview.setBackgroundResource(R.drawable.list_selector);

    convertView.setTag(holder);
} else {
    holder = (Holder) convertView.getTag();
}

所以,android.R.layout.simple_list_item_1是用于列表项的布局。它只包含一个TextView. 要添加ImageView您应该使用自定义列表项布局,例如这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight">

    <ImageView
        android:id="@+id/pic"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/your_custom_image"
        android:scaleType="centerCrop"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="8dp"/>
</LinearLayout>

将 XML 文件放入res\layout目录并命名custom_list_item.xml。然后只需在您的代码中替换android.R.layout.simple_list_item_1R.layout.custom_list_item和。android.R.id.text1R.id.text

就这样!动画保持不变。

如果您想更改每个列表项的图像,那么您还应该:

  • ImageView imageView字段添加到Holder类中
  • 添加

    holder.imageView = (ImageView) convertView.findViewById(R.id.pic);

    holder.textview.setBackgroundResource(R.drawable.list_selector);

  • 添加

    holder.imageView.setImage(R.drawable.my_drawable);

    holder.textview.setText(str);

如果需要,更改R.drawable.my_drawable为您的自定义可绘制对象。

于 2013-10-07T16:39:55.633 回答