我有片段DashboardFragment是tabhost包含片段AllDeviceFragment
后退是DashboardFragment上的按钮。 (AllDeviceFragmen是标签) AllDeviceFragment包含recycleview列表 当我点击一个项目时,它显示另一个列表,然后单击后退按钮如何显示旧列表。感谢
DashboardFragment
public class DashboardFragment扩展了Fragment {
private FragmentTabHost mTabHost;
private ImageButton imbBack;
private ImageButton imbHome;
private TextView tvPositionName;
private String mPositionName;
SharedPreferences sp;
private int mPositionId;
public static final String SharePreDashBoard = "SharePreDashBoard";
// declare interface here to back press button click;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Fragment 1", "onCreate");
/* if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}*/
SharedPreferences sharedPreferences = getActivity().getSharedPreferences(PositionFragment.SharePrePosition, Context.MODE_PRIVATE);
mPositionName = sharedPreferences.getString("PositionName", "");
// mPositionId = .getInt("POSITIONID",1);
}
public DashboardFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("Fragment 1", "onCreateView");
View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
imbBack = view.findViewById(R.id.imb_back);
imbHome = view.findViewById(R.id.imb_home);
tvPositionName = view.findViewById(R.id.tv_position_name);
mTabHost = view.findViewById(android.R.id.tabhost);
mTabHost.setup(this.getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("position_TAG").setIndicator("Location"),
PositionFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("device_TAG").setIndicator("Devices"),
DeviceFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("allDevice_TAG").setIndicator("All Devices"),
AllDeviceFragment.class, null);
/**
* set onClick for ImageButton Back*/
imbBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onClickBack();
}
});
AllDeviceFragment
public class AllDeviceFragment extends Fragment实现DeviceAdapter.OnItemClickListener,TestMuiltiAdapter.OnItemClickListenerDevice {
RecyclerView recyclerView;
SearchView searchView;
ArrayList<String> dataDevice;
private ArrayList<DataDevice> mDeviceList;
private ArrayList<Object> mIoList;
public static final String ShareDevice = "ShareDevice";
private DeviceAdapter deviceAdapter;
private TestMuiltiAdapter testAdapter;
private ApiInterface mApiService;
private SharedPreferences sharedPreferences;
private String strTokent;
private ArrayList<Data> itemArrayList;
HashMap<String, Integer> hashMapDevice;
AdminAgentActivity mActivity;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = (AdminAgentActivity) activity;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mApiService = ApiClient.getClient().create(ApiInterface.class);
mDeviceList = new ArrayList<>();
dataDevice = new ArrayList<>();
hashMapDevice = new HashMap<>();
itemArrayList = new ArrayList<>();
mIoList = new ArrayList<>();
sharedPreferences = getActivity().getSharedPreferences(LoginActivity.MyPREFERENCES, Context.MODE_PRIVATE);
strTokent = sharedPreferences.getString("TOKEN", "");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_all_device, container, false);
recyclerView = view.findViewById(R.id.recyclview_all_device);
searchView = view.findViewById(R.id.sview_all_device);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
deviceAdapter = new DeviceAdapter(mDeviceList, this);
// recyclerViewPosition.setAdapter(positionAdapter);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
final List<DataDevice> filteredModelList = filter(mDeviceList, newText);
deviceAdapter.setFilter(filteredModelList);
return true;
}
});
// Call<Device> call = mApiService.getDeviceList2(strTokent, 2);
Call<Device> call = mApiService.getDeviceList(strTokent);
call.enqueue(new Callback<Device>() {
@Override
public void onResponse(Call<Device> call, Response<Device> response) {
if (response.isSuccessful()) {
dataDevice.clear();
mDeviceList.clear();
ArrayList<ItemsDevice> itemsDeviceArrayList = response.body().getDataDevice().getItemsDevices();
/* add data to hashmap device*/
for (int j = 0; j < itemsDeviceArrayList.size(); j++) {
String deviceName = itemsDeviceArrayList.get(j).getNameDevice();
int deviceId = itemsDeviceArrayList.get(j).getId();
// Message.message(getActivity().getApplicationContext(),String.valueOf(deviceId));
hashMapDevice.put(deviceName, deviceId);
}
/*add data to mdevicelist*/
for (int i = 0; i < itemsDeviceArrayList.size(); i++) {
dataDevice.add(itemsDeviceArrayList.get(i).getNameDevice());
}
for (String strDevice : dataDevice) {
mDeviceList.add(new DataDevice(strDevice));
}
recyclerView.setAdapter(deviceAdapter);
}
}
@Override
public void onFailure(Call<Device> call, Throwable t) {
}
});
return view;
}
private List<DataDevice> filter(List<DataDevice> models, String query) {
query = query.toLowerCase();
final List<DataDevice> filteredModelList = new ArrayList<>();
for (DataDevice model : models) {
final String text = model.getStrNameDevice().toLowerCase();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
return filteredModelList;
}
@Override
public void onItemClick(int position) {
testAdapter = new TestMuiltiAdapter(mIoList, this);
String mDeviceName = "";
int device_id = 1;
try {
mDeviceName = mDeviceList.get(position).getStrNameDevice();
device_id = hashMapDevice.get(mDeviceName);
} catch (Exception e) {
e.getStackTrace();
}
mDeviceList.clear();
Call<Io> call = mApiService.getIOList(strTokent, device_id);
call.enqueue(new Callback<Io>() {
@Override
public void onResponse(Call<Io> call, Response<Io> response) {
itemArrayList.clear();
if (response.isSuccessful()) {
mIoList.clear();
itemArrayList = response.body().getData();
for (int i = 0; i < itemArrayList.size(); i++) {
int dataType = itemArrayList.get(i).getData_type();
String nameIo = itemArrayList.get(i).getName();
if (dataType == 1) {
mIoList.add(new AnalogType(nameIo, itemArrayList.get(i).getCurrent_value()));
}
if (dataType == 2) {
mIoList.add(new DigitalType(nameIo));
}
if (dataType == 3) {
mIoList.add(new StringType(nameIo, itemArrayList.get(i).getCurrent_value()));
}
if (dataType == 4) {
mIoList.add(new MultiChoiceType(nameIo));
}
}
recyclerView.setAdapter(testAdapter);
}
}
@Override
public void onFailure(Call<Io> call, Throwable t) {
}
});
/*Toast.makeText(getActivity(), mPosition, Toast.LENGTH_SHORT).show();
SharedPreferences sharePosition = getActivity().getSharedPreferences(SharePosition, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharePosition.edit();
editor.putString("POSITION", mPosition);
editor.commit();
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container_device, new DeviceOfPositionFragment())
.commit();*/
}
}
适配器
公共类DeviceAdapter扩展了RecyclerView.Adapter {
private List<DataDevice> deviceList = new ArrayList<>();
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position1);
}
public DeviceAdapter(List<DataDevice> deviceList, OnItemClickListener mListener) {
this.deviceList = deviceList;
this.mListener = mListener;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_position_row, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
DataDevice mDeviceList = deviceList.get(position);
holder.tvListPosition.setText(mDeviceList.getStrNameDevice());
holder.tvListPosition.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onItemClick(position);
}
}
});
}
@Override
public int getItemCount() {
if (deviceList == null) {
return 0;
} else
return deviceList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvListPosition;
public MyViewHolder(View itemView) {
super(itemView);
tvListPosition = (TextView) itemView.findViewById(R.id.list_position);
}
}
public void setFilter(List<DataDevice> devices) {
deviceList = new ArrayList<>();
deviceList.addAll(devices);
notifyDataSetChanged();
}
}