gpt4 book ai didi

java - 查看页面 : Refresh fragment views on user swipe

转载 作者:行者123 更新时间:2023-11-30 00:58:21 24 4
gpt4 key购买 nike

我的 MainActivity 使用 viewPager 并根据用户滑动手势实例化两个 fragment View 。其中一个 fragment 从服务器获取一些数据并更新其 View 。

问题:

在没有互联网连接的情况下启动应用程序, fragment A 更新了一些虚拟数据,但是在应用程序运行时切换 Wi-Fi 并滑动到该 fragment 不会更新 fragment 与接收到的数据。

请建议如何在实例化后更新 fragment View 。

主 Activity .java

public class MainActivity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return fragment1.newInstance();
case 1:
return fragment3.newInstance();
default:
return fragment1.newInstance();
}
}

@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
}

FragmentA类(代码中的Fragment3)给出如下:

public class fragment3 extends Fragment implements YourFragmentInterface{

public static ListView mylistView;
public static ConfessionsAdapter adapter;
public static ArrayList<Confession> confessionList;

public static Context context;

public static fragment3 newInstance() {

fragment3 fragment = new fragment3();

new HttpGetReq().execute("http://example.com");

return fragment;
}

public fragment3() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

context = getActivity(); // Getting context to tell async task about it, so it can SET stuff here.

if (container == null) {
return null;
}
View view = inflater.inflate(R.layout.list_fragment, container, false);
mylistView = (ListView) view.findViewById(android.R.id.list);

new HttpGetReq().execute("http://example.com");

return view;
}
}

HttpGetReq 类是一个AsyncTask,它从服务器获取数据,解析 JSON 并在 doInBackground 中更新 fragmentA 的 list () 方法,然后在其 onPostExecute() 方法中将适配器设置为 fragmentA 的列表 View

最佳答案

首先创建一个方法来检查您的互联网连接。对我来说,我创建了一个名为 Utils 的类,如果互联网可用,该类中有一个方法将返回 true

public class Utils {

public static boolean isNetworkAvailable(Context context) {
try {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
// if no network is available networkInfo will be null
// otherwise check if we are connected
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
} catch (Exception ex) {
return false;
}
}

}

现在创建一个Broadcast Receiver,它会在互联网连接状态改变时通知您。如果它更改为可用的互联网,那么它将通过本地 broadcast manager

发送广播

这是广播接收器的代码,

public class NetworkChangedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

if (Utils.isNetworkAvailable(context))
{
//sending local broadcast
Intent i = new Intent("net_connected");
i.putExtra("message", true);
LocalBroadcastManager.getInstance(context).sendBroadcast(i);

}
}
}

然后在 manifest 中注册你的接收器

  <receiver android:name=".NetworkChangedReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>

然后在您的 Activity 中注册您的本地广播接收器,您应该在 onResume 中注册并在 onPause 中注销它,就像这样

 @Override
public void onResume() {
super.onResume();

LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("custom-event-name"));
}

@Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
}

并在 fragment 中编写本地广播接收器的功能

   private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean isConnectTed = intent.getBooleanExtra("net_connected",false);
if(isConnectTed){
//if connected then notify to the
mSectionsPagerAdapter.notifyDataSetChanged();
}
}
};

关于java - 查看页面 : Refresh fragment views on user swipe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39816662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com