Thursday, February 9, 2017

customized list view and helper APIs

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
    private static final String TAG = "ListAdapter";
    Context mContext;
    Activity mActivity;

    public CustomListViewAdapter(Context context, Activity activity, int resourceId,
                                 List<RowItem> items) {
        super(context, resourceId, items);
        this.mContext = context;
        this.mActivity = activity;
    }

    /*private view holder class*/
    private class ViewHolder {
        ImageView imageView;
        TextView txtTitle;
        TextView txtDesc;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        RowItem rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) mContext
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.video_list_layout, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
            holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.imageView.setImageResource(rowItem.getImageId());
        holder.txtTitle.setText(rowItem.getTitle());
        holder.txtDesc.setText(rowItem.getDesc());

        return convertView;
    }

    RowItem getRowItem(int index) {
        ListView selectList = (ListView) mActivity.findViewById(R.id.video_list_view);
        ListAdapter lAdapter = selectList.getAdapter();
        RowItem item = (RowItem) lAdapter.getItem(index);
        Log.i(TAG, "item.link:" + item.getLink());

        return item;
    }

    int getItemIndex(String title) {
        int index;
        ListView selectList = (ListView) mActivity.findViewById(R.id.video_list_view);
        ListAdapter adapter = selectList.getAdapter();

        for (index = 0; index < adapter.getCount(); index++) {
            RowItem item = (RowItem) adapter.getItem(index);
            if (item.getTitle().matches(title)) {
                break;
            }
        }

        if (index == adapter.getCount())
            return -1;

        return index;
    }

    public void setImageByIndex(TypedArray images, int index, int image_index) {
        ListView listView = (ListView) mActivity.findViewById(R.id.video_list_view);
        ListAdapter adapter = listView.getAdapter();

        int wantedPosition = index; // Whatever position you're looking for
        int firstPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount(); // This is the same as child #0
        int wantedChild = wantedPosition - firstPosition;

        if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
            Log.w(TAG, "Unable to get view for desired position, because it's not being displayed on screen.");
            return;
        }

        View wantedView = listView.getChildAt(wantedChild);
        ViewHolder holder = (ViewHolder) wantedView.getTag();
        holder.imageView.setImageResource(images.getResourceId(image_index, 0));
    }
}

No comments:

Post a Comment