android只实现ListFragmentInteractionListener,但不工作
我创建了一个活动为空的简单应用程序,然后(通过向导)创建了一个碎片列表 之后,我在活动中添加了OnListFragmentInteractionListener的实现,然后添加了一个经典的Toast来验证DummyItems上的OnClick。一切都好!好 现在,我改变了模型,并在我的活动中实现了其他东西。现在OnListFragmentInteractionListener不工作 我创建了一个带有空活动和fragmentlist的secondo项目,我比较了两个项目的代码,关于fragmentlist的实现似乎都还可以 调试时我注意到Fragment.java mListener的onCreateView仍然为空。 在我的第二个项目中,它被实例化了。 你知道我应该在哪里寻找问题吗android只实现ListFragmentInteractionListener,但不工作,android,android-fragments,listener,Android,Android Fragments,Listener,我创建了一个活动为空的简单应用程序,然后(通过向导)创建了一个碎片列表 之后,我在活动中添加了OnListFragmentInteractionListener的实现,然后添加了一个经典的Toast来验证DummyItems上的OnClick。一切都好!好 现在,我改变了模型,并在我的活动中实现了其他东西。现在OnListFragmentInteractionListener不工作 我创建了一个带有空活动和fragmentlist的secondo项目,我比较了两个项目的代码,关于fragment
public class OptionsActivity extends AppCompatActivity
implements DeviceBTFragment.OnListFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
Typeface font_caption = Typeface.createFromAsset(getAssets(),"fonts/univers_condensed_bold.ttf");
TextView textView_caption = (TextView) findViewById(R.id.caption_fragment);
textView_caption.setTypeface(font_caption);
}
@Override
public void onListFragmentInteraction(DeviceBT item) {
//DisplayMetrics metrics = getResources().getDisplayMetrics();
//String message = Float.toString(metrics.density);
Toast toast = Toast.makeText(this,item.name,Toast.LENGTH_SHORT);
toast.show();
Intent intent = new Intent(this,DeviceBTCardActivity.class);
intent.putExtra("DEVICE_NAME", item.name);
intent.putExtra("DEVICE_ADDRESS",item.address);
startActivity(intent);
}
}
公共类DeviceBTFragment扩展了片段{
私有静态最终字符串ARG\u COLUMN\u COUNT=“COLUMN COUNT”;
私有静态最终整数请求_ENABLE_BT=1;
私有int mColumnCount=1;
私有OnListFragmentInteractionListener-MLListener;
/**
*片段管理器实例化
*碎片(如屏幕方向改变时)。
*/
公用设备btfragment(){}
@抑制警告(“未使用”)
公共静态设备BTFragment newInstance(int columnCount){
DeviceBTFragment=新的DeviceBTFragment();
Bundle args=新Bundle();
args.putInt(ARG\u COLUMN\u COUNT,columnCount);
fragment.setArguments(args);
返回片段;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(getArguments()!=null){
mColumnCount=getArguments().getInt(ARG\u COLUMN\u COUNT);
}
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u devicebt\u列表,容器,false);
//设置适配器
如果(查看RecyclerView的实例){
Context=view.getContext();
RecyclerView RecyclerView=(RecyclerView)视图;
如果(mColumnCount 0){
//循环通过配对设备
用于(蓝牙设备:pairedDevices){
//将名称和地址添加到要在列表视图中显示的阵列适配器
DeviceBT deviceItem=新的DeviceBT(mIndex,device.getName(),device.getAddress());
添加(设备项);
mIndex++;
}
}
}
返回斜纹螨;
}
@凌驾
公共void-onAttach(上下文){
super.onAttach(上下文);
if(OnListFragmentInteractionListener的上下文实例){
mListener=(OnListFragmentInteractionListener)上下文;
}否则{
抛出新的RuntimeException(context.toString()
+“必须实现OnListFragmentInteractionListener”);
}
}
@凌驾
公共无效连接(){
super.onDetach();
mListener=null;
}
/**
*此接口必须由包含以下内容的活动实现
*片段,以允许通信此片段中的交互
*该活动以及其中可能包含的其他片段
*活动。
*
*有关更多信息,请参阅Android培训课程。
*/
公共接口OnListFragmentInteractionListener{
无效仅限ListFragmentInteraction(设备BT项);
}
}您的
活动是否实现了OnFragmentInteractionListener
?请显示活动和片段的完整源代码是!我发布了一个代码。好的,我更正了我的帖子,对不起!
public class DeviceBTFragment extends Fragment {
private static final String ARG_COLUMN_COUNT = "column-count";
private static final int REQUEST_ENABLE_BT = 1;
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public DeviceBTFragment() { }
@SuppressWarnings("unused")
public static DeviceBTFragment newInstance(int columnCount) {
DeviceBTFragment fragment = new DeviceBTFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_devicebt_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new DeviceBTRecyclerViewAdapter(GetItems(), mListener));
}
return view;
}
private List<DeviceBT> GetItems()
{
List<DeviceBT> mItems = new ArrayList<DeviceBT>();
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
else
{
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
int mIndex = 0;
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
DeviceBT deviceItem = new DeviceBT(mIndex,device.getName(),device.getAddress());
mItems.add(deviceItem);
mIndex++;
}
}
}
return mItems;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnListFragmentInteractionListener {
void onListFragmentInteraction(DeviceBT item);
}