带动画行的导航抽屉

时间:2015-07-12 17:02:02

标签: java android multithreading android-layout android-handler

我想在Android中设计navigation drawer like this one。所以,我试过了。

我的ListView包含用于导航抽屉的ImageView和TextView的行。现在,我想在导航抽屉中制作动画文本。所以,我尝试在我的适配器中使用Handler

DrawerModel.java

public class DrawerModel {

    private int icon;
    private String title;

    public DrawerModel(){
        icon = R.drawable.ic_settings_white_24dp;
        title = "";
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

DrawerAdapter.java

public class DrawerAdapter extends BaseAdapter{
    private Context mContext;
    private List<DrawerModel> mListDrawerModels;
    private static Handler mHandler;

    public DrawerAdapter(Context context,List<DrawerModel> listDrawerModels){
        mContext = context;
        mListDrawerModels = listDrawerModels;
        mHandler = new Handler();
    }

    @Override
    public int getCount() {
        return mListDrawerModels.size();
    }

    @Override
    public DrawerModel getItem(int position) {
        return mListDrawerModels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row;
        ViewHolder holder;

        if(convertView==null){
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.row_listview_drawer, parent, false);

            holder = new ViewHolder(mContext,row);
            row.setTag(holder);
        }
        else{
            row = convertView;
            holder = (ViewHolder) row.getTag();
        }

        if(mListDrawerModels !=null && !mListDrawerModels.isEmpty()) {
            DrawerModel model = mListDrawerModels.get(position);
            if (model != null) {
                holder.ivDrawerRow.setImageResource(model.getIcon());
                DrawerAdapter.setTextAsync(position, holder, model);
            }
        }
        return row;
    }

    private static void setTextAsync(int position, final ViewHolder holder, final DrawerModel model) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                holder.tvDrawerRow.setText(model.getTitle());
            }
        },position*1000);
    }

    private static class ViewHolder{
        private ImageView ivDrawerRow;
        private TextView tvDrawerRow;

        public ViewHolder(Context context,View row){
            this.ivDrawerRow = (ImageView) row.findViewById(R.id.iv_row_drawer);
            this.tvDrawerRow = (TextView) row.findViewById(R.id.tv_row_drawer);
            this.tvDrawerRow.setTypeface(FontCache.getInstance(context).getTypeFace());
        }
    }
}

在这里,我的适配器内部,setTextAsync方法;我试图将延迟与位置相乘,这很好但文本没有动画。他们只是在几秒钟之后逐一出现。此外,这仅适用于第一次。

我该怎么做?谁能指导我?

1 个答案:

答案 0 :(得分:0)

要提供textView动画,有更好的方法

https://github.com/daimajia/AndroidViewAnimations