마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

클래스를 분리시키고 싶은데요~ 가능할까요?

0 추천
이 소스를 좀 더 간결하게하기 위해서
자바클래스에서 spinner 부분하고 listview 부분을 덜어내고 싶은데
어떻게 클래스를 나눠야할지 아직 감이 안옵니다.
힌트 좀 부탁드립니다~
(나름대로 나눴는데 에러만 생기더군요..)
(제가 나눈 방법 >> http://www.androidside.com/B49/124863 )
menu_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="@+id/menuBtn"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#00000000"
android:src="@drawable/menubutton" />
<RelativeLayout
android:id="@+id/slidingPage01"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ffffff"
android:visibility="gone" >
<Spinner
android:id="@+id/menuSpin"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Spinner>
<ListView
android:id="@+id/menuList"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@id/menuSpin"
android:textColor="#ffff0000" >
</ListView>
</RelativeLayout>
</RelativeLayout>
 
menuView.java
 
package com.example;
import java.util.ArrayList;
import java.util.Arrays;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;
public class MenuView extends RelativeLayout implements OnClickListener, AnimationListener{
Context mContext;
ImageButton menuBtn;
boolean isPageOpen = false;

Animation translateLeftAnim;
Animation translateRightAnim;
RelativeLayout menuPage;
MenuOfSpinnerView spinnerView;
MenuOfListView listView;

Spinner newsSpinner;
ArrayAdapter<String> adapterForSpinner; // 스피너용 어뎁터
String[] press = { "A", "B", "C" }; // 스피에 들어갈 언론사들
ListView subjectList;
ArrayAdapter<String> adapterForList; // 리스트용 어뎁터
ArrayList<String> arrayList; // 리스트뷰에 들어갈 주제들

public MenuView(Context context) {
super(context);
init(context);
}
public MenuView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public void init(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.menu_view, this, true);

mContext = context;
menuBtn = (ImageButton) findViewById(R.id.menuBtn);
menuBtn.setOnClickListener(this);

translateLeftAnim = AnimationUtils.loadAnimation(mContext, R.anim.translate_left);
translateRightAnim = AnimationUtils.loadAnimation(mContext, R.anim.translate_right);
translateLeftAnim.setAnimationListener(this);
translateRightAnim.setAnimationListener(this);
menuPage = (RelativeLayout) findViewById(R.id.slidingPage01);

newsSpinner = (Spinner) findViewById(R.id.menuSpin);
adapterForSpinner = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, press);
newsSpinner.setAdapter(adapterForSpinner);
adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_item);
newsSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String str = (String) adapterForSpinner.getItem(arg2);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//요 두부분을 각각의 클래스로 분리시키고 싶습니다.
subjectList = (ListView) findViewById(R.id.menuList);
arrayList = new ArrayList<String>(Arrays.asList("aa", "bb", "cc","dd", "ee", "ff", "gg", "hh", "ii"));
adapterForList = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, arrayList);
subjectList.setAdapter(adapterForList);
subjectList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
subjectList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String str = (String) adapterForList.getItem(arg2);
if (str.equals("aa")) {
} else if (str.equals("bb")) {
} else if (str.equals("cc")) {
} else {
Toast.makeText(mContext,
"Selected : " + str, 2000).show();
}
}
});
}//END init()
public void onClick(View view) {
switch(view.getId()){
case R.id.menuBtn:
if (isPageOpen) {
menuPage.startAnimation(translateLeftAnim);
} else {
menuPage.setVisibility(View.VISIBLE);
menuPage.startAnimation(translateRightAnim);
}
break;
}//END switch()
}//End onClick()

public void onAnimationEnd(Animation animation) {
if (isPageOpen) {
menuPage.setVisibility(View.INVISIBLE);
isPageOpen = false;
} else {
isPageOpen = true;
}
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
}//END CLASS MenuView

 

뇽석 (530 포인트) 님이 2013년 8월 16일 질문

2개의 답변

0 추천
Nope, not true. Once you pay for an Android app, it's yours. If you decide to untlniasl the app or want to reset your phone, you can download the app again without charge. It will show up as Purchased in your apps list (even if it's not currently downloaded to your phone).One nice thing as compared to the iPhone is that you have a 24 hour trial period for all paid Android apps. If you decide you don't want it or like it, you can get a full refund.Enjoy your Android apps! http://kmosmshhli.com [url=http://dcwpae.com]dcwpae[/url] [link=http://ceqbzxtiw.com]ceqbzxtiw[/link]
익명사용자 님이 2014년 2월 4일 답변
0 추천

어려울게 전혀 없는데요.

클래스를 새로 만드시고 생성자에 Activity 를 받게 끔 하신후에

분리하고자 하는 부분의 소스를 작성하여 주시면됩니다.

아래는 대충 써본건데 어느정도 이해가 되셨으면 좋겠네요.

 


class A {

private Spinner newsSpinner;
private ArrayAdapter<String> adapterForSpinner;

// 생성자
public A (Activity activity) {

newsSpinner = (Spinner) activity.findViewById(R.id.menuSpin);
adapterForSpinner = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item, press);
newsSpinner.setAdapter(adapterForSpinner);
adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_item);
newsSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String str = (String) adapterForSpinner.getItem(arg2);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});

}

 

아픔제로 (3,460 포인트) 님이 2014년 2월 4일 답변
...