Android Open Source - oidarSample Drawer Adapter






From Project

Back to project page oidarSample.

License

The source code is released under:

GNU General Public License

If you think the Android project oidarSample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.oidar.adapter;
//from w ww  .  j ava  2s . co  m
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.oidar.R;
import com.oidar.model.DrawerListItem;

import java.util.ArrayList;

/**
 * Adapter for the items in the NavigationDrawer.
 */
public class DrawerAdapter extends BaseAdapter {

    private final LayoutInflater mInflater;
    private final ArrayList<DrawerListItem> mListItems;
    private int mSelectedIndex = -1;

    /**
     * Constructor for the adapter.
     */
    public DrawerAdapter(Context context, ArrayList<DrawerListItem> listItems) {
        mListItems = listItems;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * Return the number of items in the list.
     */
    @Override
    public int getCount() {
        return mListItems.size();
    }

    /**
     * Return the item at a given position.
     */
    @Override
    public Object getItem(int position) {
        return mListItems.get(position);
    }

    /**
     * Return the item id of a given position.
     */
    @Override
    public long getItemId(int position) {
        return 0;
    }

    /**
     * Return the view of the item.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.drawer_list_item, parent, false);
            holder.text = (TextView) convertView.findViewById(R.id.text);
            holder.smallText = (TextView) convertView.findViewById(R.id.textViewSmall);
            holder.selector = convertView.findViewById(R.id.selection);
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            holder.bigLayout = convertView.findViewById(R.id.bigLayout);
            holder.smallLayout = convertView.findViewById(R.id.smallLayout);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        switch (mListItems.get(position).getType()) {
            case REGULAR:
                holder.smallLayout.setVisibility(View.GONE);
                holder.text.setText(mListItems.get(position).getTitle());
                int visibility = mSelectedIndex == position ? View.VISIBLE : View.INVISIBLE;
                holder.selector.setVisibility(visibility);
                holder.bigLayout.setVisibility(View.VISIBLE);
                break;
            case SMALL:
                holder.smallText.setText(mListItems.get(position).getTitle());
                holder.bigLayout.setVisibility(View.GONE);
                holder.icon.setImageResource(mListItems.get(position).getIconRes());
                holder.icon.setVisibility(View.VISIBLE);
                holder.smallLayout.setVisibility(View.VISIBLE);
                break;
        }

        return convertView;
    }

    /**
     * Return if a given position is selected. If it's not selected select it.
     */
    public boolean isPositionSelected(int position) {
        boolean isSelected = true;

        if (mSelectedIndex != position) {
            isSelected = false;
            mSelectedIndex = position;
            notifyDataSetChanged();
        }
        return isSelected;
    }

    /**
     * ViewHolder used to increase performance.
     */
    private static class ViewHolder {
        TextView text;
        View selector;
        ImageView icon;
        TextView smallText;
        View bigLayout;
        View smallLayout;
    }
}




Java Source Code List

com.oidar.ApplicationTest.java
com.oidar.activity.MainActivity.java
com.oidar.activity.SettingsActivity.java
com.oidar.activity.base.BaseActivity.java
com.oidar.adapter.DrawerAdapter.java
com.oidar.adapter.DrawerLiveNewsAdapter.java
com.oidar.adapter.DrawerTalkRadioAdapter.java
com.oidar.animation.CustomObjectAnimator.java
com.oidar.animation.DepthPageTransformer.java
com.oidar.animation.ZoomOutPageTransformer.java
com.oidar.fragment.AboutFragment.java
com.oidar.fragment.DrawerLiveNewsFragment.java
com.oidar.fragment.DrawerTalkRadioFragment.java
com.oidar.fragment.FeedbackFragment.java
com.oidar.fragment.LiveNewsFragment.java
com.oidar.fragment.SettingsFragment.java
com.oidar.fragment.TalkRadioFragment.java
com.oidar.fragment.base.DrawerFragment.java
com.oidar.fragment.base.RadioFragment.java
com.oidar.model.DrawerListItem.java
com.oidar.model.ListItemType.java
com.oidar.model.Radio.java
com.oidar.sql.SqlHandler.java
com.oidar.util.Constants.java
com.oidar.util.MathHelper.java
com.oidar.util.MyLog.java
com.oidar.util.OIDARConstants.java
com.oidar.util.PreferenceUtil.java
com.oidar.util.StringHelper.java
com.oidar.util.Util.java
com.oidar.view.CustomDrawerLayout.java