17

この質問に対する答えを求めて Web を探しましたが、Android の公式ドキュメントでも答えが見つからないようです。

Android の新しいナビゲーション ドロワー レイアウトを実装するアプリを作成中です。このレイアウトのドキュメントは、ここここにあります。

設計ドキュメントだけでなく、Facebook や Google Currents などの多くの一般的なアプリでも、ドロワー アイテムの前にアイコンが表示されています。

設計ドキュメント自体は、「ナビゲーション ドロワーのコンテンツ」の下にそれらについて言及しています。

ナビゲーション ターゲットには、オプションの先頭のアイコンと末尾のカウンターを含めることができます。カウンターを使用して、対応するビューのデータの状態が変化したことをユーザーに通知します。

残念ながら、開発者向けドキュメントには、この設定でアイコンを実装する方法については言及されていません。アイテムのリストを次のように実装する方法について説明します。

public class MainActivity extends Activity {
private String[] mPlanetTitles;
private ListView mDrawerList;
...

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mPlanetTitles = getResources().getStringArray(R.array.planets_array);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mPlanetTitles));
    // Set the list's click listener
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    ...
}
}

それらの実装は getStringArray() を使用しているため、行項目の xml を変更してアイコンを含める方法は考えられません。

多くのアプリは各テキスト項目の前にアイコンを実装しているようで、デザインのドキュメントではアイコンの追加についても言及されているため、これは不可解です。これらのアイコンを追加するには、簡単な API 呼び出しが必要だと思いますが、これまでに見つけた唯一の実装は、とてつもなく複雑に思えます。

Navigation Drawer アイテムにアイコンを追加する方法を知っている人はいますか? 単純な API 呼び出しでこれを行う方法はありますか、またはこれらすべての企業 (Facebook、Google など) が問題を回避する方法を見つけなければなりませんでしたか?

みんな、ありがとう。

4

4 に答える 4

7

それらの実装は getStringArray() を使用しているため、行項目の xml を変更してアイコンを含める方法は考えられません。

のモデル データの選択は、それによって作成される行がアイコンを持つことができるListAdapterかどうかにはまったく影響しません。ListAdapter

Navigation Drawer アイテムにアイコンを追加する方法を知っている人はいますか?

ImageViewの行レイアウトに を入れますListAdapter。あなたの をオーバーライドするなどして、あなたのをImageView介してそれを設定します。IOW、何年も行われてきたように、他の .ListAdaptergetView()ArrayAdapterListView

簡単な API 呼び出しでこれを行う方法はありますか

それは、「単純」および「API 呼び出し」の定義によって異なります。

于 2013-06-12T15:51:49.067 に答える
3

次のようなアダプタ クラスを作成します。

public class AdapterClass  extends ArrayAdapter<String> {
Context context;
private ArrayList<String> TextValue = new ArrayList<String>();

public AdapterClass(Context context, ArrayList<String> TextValue) {
    super(context, R.layout.row_of_drawer, TextValue);
    this.context = context;
    this.TextValue= TextValue;

}


@Override
public View getView(int position, View coverView, ViewGroup parent) {
    // TODO Auto-generated method stub

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.row_of_drawer,
            parent, false);

    TextView text1 = (TextView)rowView.findViewById(R.id.text1);
    text1.setText(TextValue.get(position));

    return rowView;

}

}

MainActivity に変更を加えます。

使用する

   AdapterClass adClass = new AdapterClass(MainActivity.this, name_of_arrayList);

    mDrawerList.setAdapter(adClass);

の代わりに

 mDrawerList.setAdapter(new ArrayAdapter<String>(this,
        R.layout.drawer_list_item, mPlanetTitles));
于 2013-07-19T07:11:11.920 に答える
2

string 配列は、行のレイアウトを定義するものではありません。YourR.layout.drawer_list_itemは、リストの各行のレイアウトを表します。画像 (またはその他の複雑な行レイアウト) を含めたい場合は、xml を置き換えるカスタム レイアウト xml に画像ビューを含めますR.layout.drawer_list_item

画像やテキストを各行に設定する方法は、すべてgetView()配列アダプターのメソッドで行われます。大まかな例は次のとおりです (行に textview と imageview があると仮定します)。

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

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.revise_listview, parent,false);

    TextView tv = (TextView)convertView.findViewById(R.id.text);
    ImageView iv = (ImageView)convertView.findViewById(R.id.image);

    tv1.setText(stringArray.get(i));
    if(//condition) {
        iv.setImageResource(R.drawable.imagexx);
    }
    return convertView;
}

各行のビューを実装するより効率的な方法は、ビューホルダー パターンを使用することです。例を見つけることができる多くの場所の 1 つがここにあります

于 2013-06-12T16:05:19.680 に答える
2

これを非常に明確に構造化するために、オブジェクト指向の実装用に数行のコードを追加しました。この実装では、NavigationItems の配列からすべてを生成します。これは、テキスト、アイコン、およびメニュー項目のフラグメントのフィールドを含むプレーンな Java クラスです。

MainActivity では、ナビゲーションは配列によって単純に定義されます。

NavigationAdapter navigationMenuAdapter;
NavigationItem[] navigationMenuItems = {
    new NavigationItem(R.string.menu_home, R.drawable.ic_menu_home, new MainFragment()),
    new NavigationItem(R.string.menu_statistics, R.drawable.ic_statistics, new StatisticsFragment()),
    new NavigationItem(R.string.menu_settings, R.drawable.ic_settings, new SettingsFragment()),
};

protected void onCreate(Bundle savedInstanceState) {
    ...
    navigationMenuAdapter = new NavigationAdapter(this, navigationMenuItems);
    mDrawerList.setAdapter(navigationMenuAdapter);
}

...
private void selectItem(int position) {
    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
                   .replace(R.id.content_frame, navigationMenuItems[position].fragment)
                   .commit();

    mDrawerList.setItemChecked(position, true);
    setTitle(navigationMenuItems[position].stringTitle);
    mDrawerLayout.closeDrawer(mDrawerList);
}

NavigationItemクラス:

public class NavigationItem
{
    /**
     * 
     * @param stringTitle The id of the string resource of the text of the item.
     * @param drawableIcon The id of the drawable resource of icon of the item.
     * @param fragment The Fragment to be loaded upon selecting the item.
     */
    public NavigationItem(int stringTitle, int drawableIcon, Fragment fragment)
    {
        this.stringTitle = stringTitle;
        this.drawableIcon = drawableIcon;
        this.fragment = fragment;
    }

    /**
     * The id of the string resource of the text of the item.
     */
    public int stringTitle;

    /**
     * The id of the drawable resource of icon of the item.
     */
    public int drawableIcon;

    /**
     * The Fragment to be loaded upon selecting the item.
     */
    public Fragment fragment;
}

そして、NavigationAdapter:

public class NavigationAdapter extends BaseAdapter {
    private Context context;
    private NavigationItem[] navigationItems;

    public NavigationAdapter(Context context, NavigationItem[] items) {
        this.context = context;
        navigationItems = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.list_item_navigation, parent, false);
        } else {
            row = convertView;
        }

        TextView tvTitle = (TextView) row.findViewById(R.id.textView);
        ImageView ivIcon = (ImageView) row.findViewById(R.id.imageView);

        tvTitle.setText(navigationItems[position].stringTitle);
        ivIcon.setImageResource(navigationItems[position].drawableIcon);
        return row;
    }
}

list_item_navigationとのみを含む非常に基本的な xml レイアウト を使用しImageViewますTextView

于 2015-03-02T21:15:19.243 に答える