DrawLayout 몇 가지 주의 점
package com.example.navigationdrawer5;
import java.util.Locale;
import android.app.Activity;
import android.app.Fragment;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
public class DrawerActivity extends Activity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ListView drawerList;
private String[] planetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
planetTitles = getResources().getStringArray(R.array.planets_array);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerList = (ListView) findViewById(R.id.drawerList);
drawerList.setAdapter(new ArrayAdapter(this, R.layout.drawer_list_item, planetTitles));
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
selectItem(position);
}
});
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
int position = drawerList.getCheckedItemPosition();
getActionBar().setTitle(planetTitles[position]);
invalidateOptionsMenu();
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(R.string.app_name);
invalidateOptionsMenu();
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(drawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
private void selectItem(int position) {
Fragment fragment = new DrawerFragment();
Bundle args = new Bundle();
args.putInt(DrawerFragment.ARG_PLANET_ID, position);
fragment.setArguments(args);
getFragmentManager().beginTransaction().replace(R.id.contentFrame, fragment).commit();
drawerList.setItemChecked(position, true);
drawerLayout.closeDrawer(drawerList);
getActionBar().setTitle(planetTitles[position]);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
//5、
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean isDrawerOpen = drawerLayout.isDrawerOpen(drawerList);
menu.findItem(R.id.action_search).setVisible(!isDrawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.drawer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//6. action bar item
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.action_search:
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, " ", Toast.LENGTH_LONG).show();
}
break;
}
return super.onOptionsItemSelected(item);
}
public static class DrawerFragment extends Fragment {
private static final String ARG_PLANET_ID = "planet_id";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ImageView view = (ImageView) inflater.inflate(R.layout.drawer_content, container, false);
Resources res = getActivity().getResources();
int planetId = getArguments().getInt(ARG_PLANET_ID);
int imageid = res.getIdentifier(res.getStringArray(R.array.planets_array)[planetId].toLowerCase(Locale.getDefault()), "drawable", getActivity().getPackageName());
view.setImageResource(imageid);
return view;
}
}
}
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:id="@+id/drawerList"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"/>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.