我有一个包含大量片段的主要活动。我在主活动中实现了导航抽屉,这样当我查看碎片时,我也可以打开导航抽屉。在main_activity.xml
布局文件(参见下文)中,我使用<include layout="@layout/navigation_drawer"/>
来包含导航抽屉的布局。在导航抽屉里面,我有很多按钮和文本可以将用户重定向到不同的片段。
目前,我正在处理主活动中导航抽屉的所有onClick事件。但是,这使得我在主活动中的代码很长并且很难管理(在可读性方面以及是否易于编辑)。如何在另一个专门处理导航抽屉事件的类中处理onClick事件?实现这一目标的最佳方法是什么?
main_activity.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer"
android:layout_height="match_parent"
android:layout_width="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<LinearLayout
android:id="@+id/drawerContent"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical">
<include layout="@layout/navigation_drawer"/>
</Linear Layout>
</android.support.v4.widget.DrawerLayout>
MainActivity类
public class MainActivity extends FragmentActivity implements View.OnClickListener, DrawerLayout.DrawerListener {
Button button1;
TextView text1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
button1 = (Button)findViewById(R.id.button1);
text1 = (TextView)findViewById(R.id.text1);
button1.setOnClickListener(this);
text1.setOnClickListener(this);
}
public void onClick(View v){
//I handle all onClick events of navigation drawer here.
//I want to move all of these to another class.
//If possible, I would like to move the onClick method and declaration of Button and TextView variables too.
}
}
答案 0 :(得分:1)
创建一个实现View.OnClickListener接口的类,并将新创建的类对象传递给widget setonclicklistner方法 公共类MainActivity extends FragmentActivity实现了View.OnClickListener,DrawerLayout.DrawerListener {
Button button1;
TextView text1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
button1 = (Button)findViewById(R.id.button1);
text1 = (TextView)findViewById(R.id.text1);
button1.setOnClickListener(new ClickHander());
}}
public class ClickHander implements View.OnClickListener
{
@Override
public void onClick(View v) {
}
}
答案 1 :(得分:0)
我建议另一种操作:MVP体系结构。
通过使用这种体系结构,您可以使用演示者将视图与逻辑分离。
目标是拥有更多包含小段代码的类,从而使它们也都可以进行单元测试和维护。
以这种方式操作:
这是一个基本示例,您可以根据需要对其进行修改。
MyActivityPresenter.java
.NET Reflector
}
MyActivityPresenterImpl.java
public interface MyActivityPresenter {
void init();
/**
* This method is for example only
* @param intent You can pass whatever you want as argument, but don't pass views
* because It would be formally uncorrect
*/
void onTextViewClicked(Intent intent);
}
MyAppBaseView.java
public class MyActivityPresenterImpl implements MyActivityPresenter {
private Activity activity;
private MyActivityPresenterView view;
public MyActivityPresenterImpl(Activity activity, MyActivityPresenterView view) {
this.activity = activity;
this.view = view;
}
@Override
public void init() {
//if(some condition or nothing at all){
view.initViews();
//}
}
@Override
public void onTextViewClicked(Intent intent) {
//Do controls in Presenter Implementation
if(intent != null){
Fragment fragment = new SomeFragment();
Bundle bundle = intent.getExtras();
if(bundle != null){
fragment.setArguments(bundle);
}
view.loadFragment(fragment);
}
}
}
MyActivityView.java
public interface MyAppBaseView {
//Just created in order to be extended from every interface and to not rewrite everytime from scratch each method
void loadFragment(Fragment fragment);
}
MyActivity.java
public interface MyActivityView extends MyAppBaseView {
void initViews();
}
这仅是示例。
形式上的想法应该是将逻辑与视图分开。从而使所有内容更具可读性,可测试性和可维护性。
希望有帮助。