Android 静态按钮侦听器

Android Static Button Listeners

基本上我正在尝试为我的 fragment_home_screen activity 设置一些按钮侦听器。我保留 运行 的唯一问题是在获取实例化按钮的正确视图时。你看,视图是静态的 class,所以当我尝试设置按钮侦听器时,任何内部调用都必须是静态对象。

到目前为止,我的应用程序有一个滑动抽屉和一个起始片段。

我想从侦听器中调用非静态方法。有什么办法解决这个问题,或者可能的解决方案吗?

[大约在中途向下滚动到 onCreateView 方法以查看问题]

 public class HomeScreen extends Activity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks {

private  BluetoothSocket mmSocket;
private  BluetoothAdapter mBluetoothAdapter;
private  BluetoothDevice mmDevice;
private  UUID MY_UUID;
private  final String NAME = "Acceptor";


private NavigationDrawerFragment mNavigationDrawerFragment;

private CharSequence mTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));

    startUpBluetooth();

}


@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
            .commit();
}

public void onSectionAttached(int number) {
    switch (number) {
        case 1:
            mTitle = getString(R.string.title_section1);
            break;
        case 2:
            mTitle = getString(R.string.title_section2);
            break;
        case 3:
            mTitle = getString(R.string.title_section3);
            break;
        case 4:
            mTitle = getString(R.string.title_section4);
            break;
        case 5:
            mTitle = getString(R.string.title_section5);
            break;
    }
}

public void restoreActionBar() {
    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {

        getMenuInflater().inflate(R.menu.home_screen, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment {
    /
    private static final String ARG_SECTION_NUMBER = "section_number";


    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home_screen, container, false);

        try {
            Button acceptButton = (Button) rootView.findViewById(R.id.acceptButton);
            Button connectedButton = (Button) rootView.findViewById(R.id.connectedButton);
            Button connectButton = (Button) rootView.findViewById(R.id.connectButton);

            acceptButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   // ***problem is here***
                    AcceptThread a = new AcceptThread();
                    a.run();
                }
            });
            connectedButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ConnectedThread cc = new ConnectedThread();
                    cc.run();
                }
            });
            connectButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ConnectThread con = new ConnectThread();
                    con.run();
                }
            });
        }
        catch(Exception e1 ){
            errorToast("We have a button problem: "+e1.toString());
        }

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((HomeScreen) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}



public class ConnectThread extends Thread {

    public ConnectThread() {
        ...
    }

    public void run() {
        ...
    }


    public void cancel() {
        ...
    }
}


public  class ConnectedThread extends Thread {
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread() {
        ...
    }

    public void run() {
       ...
    }


    public void write(byte[] bytes) {
       ...
    }


    public void cancel() {
        ...
    }
}

//this class is in the same file
public class AcceptThread extends Thread {
    private final BluetoothServerSocket mmServerSocket;

    public AcceptThread() {
        ...
    }

    public void run() {
        ...
    }

    public void cancel() {
        ....
    }
}

}

这里是 XML:

 Fragment_home_screen.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".HomeScreen$PlaceholderFragment">

<TextView android:id="@+id/largeText" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Connected Thread"
    android:id="@+id/acceptButton"
    android:layout_centerVertical="true"
    android:layout_alignParentStart="true"
    android:layout_alignEnd="@+id/connectButton" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Connect Thread"
    android:id="@+id/connectedButton"
    android:layout_below="@+id/acceptButton"
    android:layout_alignEnd="@+id/acceptButton"
    android:layout_toEndOf="@+id/largeText"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Accept Thread"
    android:id="@+id/connectButton"
    android:layout_above="@+id/acceptButton"
    android:layout_alignParentEnd="true"
    android:layout_toEndOf="@+id/largeText" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView"
    android:layout_alignEnd="@+id/connectButton"
    android:layout_above="@+id/connectButton"
    android:layout_toEndOf="@+id/largeText"
    android:layout_below="@+id/largeText" />

 Activity_home_screen.xml

 <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".HomeScreen">

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.gregorybolet.scoutingapp.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

I would like to call a non-static method from within the listener. Is there any way around this, or a possible solution?

要访问 AcceptThreadConnectedThread classes :

选项 1. 使两个 classes static 因为我们只能访问静态方法和静态范围内的 classes

选项 2.PlaceholderFragment 中删除 static class

同时更改newInstance方法:

public   PlaceholderFragment newInstance(int sectionNumber) {

       return fragment;
    }

并在 FragmentManager 中添加时使用 new 获取 PlaceholderFragment class 的对象:

fragmentManager.beginTransaction()
            .replace(R.id.container,new PlaceholderFragment.newInstance(
                                                             position + 1))
            .commit();

第二个选项很简单,而不是使所有 classes 静态化,只需从 PlaceholderFragment class

中删除 static

我不确定静态 class 是否是最好的方式。

一些使它更容易的建议:

  1. 使 PlaceholderFragment 不是静态的;
  2. 使用常规构造函数实例化片段:PlaceholderFragment()
  3. 使用setArguments(Bundle extras)方式传位

基本上,newInstance 代码正在执行的任何操作都发生在外部。

或者...

为您的片段创建一个新的构造函数:

public PlaceholderFragment(int sectionNumber) {
    this();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    setArguments(args);
}

绝不是@Override 默认构造函数。 Android 在需要时使用它重新创建片段。

创建这个新的构造函数将允许您这样做:

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fm = getFragmentManager();
    fm.beginTransaction().replace(R.id.container, 
        new PlaceholderFragment(position + 1)).commit();
}