gpt4 book ai didi

Android - 在选项卡式 fragment 中拉动刷新

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

我试图在 ListView 顶部向下滑动时进行刷新。我关注了this link使其工作并且它在 Activity 中起作用。但是我需要在主要 Activity 中标记的 fragment 上使用它,所以我不能如示例所示在我的 fragment 上使用此方法。

这是使用 Activity 时的正确代码:

TestActivity.java

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Fragment;
import android.app.ListFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class TestActivity extends BaseActivity {

@Override
protected Fragment getSampleFragment() {
return new SimpleListFragment();
}

public static class SimpleListFragment extends ListFragment implements
OnRefreshListener {

int i = 0;

private PullToRefreshLayout mPullToRefreshLayout;

ArrayAdapter<String> adapter;

List<String> list;

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);

list = new ArrayList<String>();
int no = 1;
for (int i = 0; i < 5; i++) {
list.add("Item No :" + no++);
}

super.onViewCreated(view, savedInstanceState);
ViewGroup viewGroup = (ViewGroup) view;

// As we're using a ListFragment we create a PullToRefreshLayout
// manually
mPullToRefreshLayout = new PullToRefreshLayout(
viewGroup.getContext());

// We can now setup the PullToRefreshLayout
ActionBarPullToRefresh
.from(getActivity())
// We need to insert the PullToRefreshLayout into the
// Fragment's ViewGroup
.insertLayoutInto(viewGroup)
// Here we mark just the ListView and it's Empty View as
// pullable
.theseChildrenArePullable(android.R.id.list,
android.R.id.empty).listener(this)
.setup(mPullToRefreshLayout);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);

adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, list);

// Set the List Adapter to display the sample items
setListAdapter(adapter);
setListShownNoAnimation(true);
}

@Override
public void onRefreshStarted(View view) {
// TODO Auto-generated method stub

// setListShown(false); // This will hide the listview and visible a
// round progress bar

new AsyncTask<Void, Void, Void>() {

@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(5000); // 5 seconds
int itemNo = list.size();
itemNo++;
list.add("New Item No :" + itemNo);

} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);

adapter.notifyDataSetChanged();
// Notify PullToRefreshLayout that the refresh has finished
mPullToRefreshLayout.setRefreshComplete();

// if you set the "setListShown(false)" then you have to
// uncomment the below code segment

// if (getView() != null) {
// // Show the list again
// setListShown(true);
// }
}
}.execute();

}

}

}

我已经多次尝试让这个方法在我的 fragment 代码中工作,但我没有成功。下面的代码是一个工作 fragment 示例,没有任何 TestActivity 输入。

InfoFragment.java

import java.util.ArrayList;
import java.util.List;

import com.example.app.Config;
...
import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

public class TicketInfoFragment extends Fragment {

List<Info> ticketInfo;
TicketFull ticket = new TicketFull();
private DatabaseHelper db;
int ticketId = TicketActivity.getCurrentTicketId();
String androidId;
String authCode;
String platform_url;
int uId;
View rootView;

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

db = new DatabaseHelper(getActivity());
androidId = Secure.getString(getActivity().getContentResolver(),
Secure.ANDROID_ID);
Config config = new Config();
config = db.getConfig(androidId);
authCode = config.getAuthCode();
platform_url = config.getPlatformURL();
uId = config.getuId();
}

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

private void fillTicket() {
HttpReader httpReader = new HttpReader();
httpReader
.setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
@Override
public void resultReady(String result) {
JsonHelper jsonHelper = new JsonHelper();
ticket = jsonHelper.getTicket(result);
//showTicket();
fillSettings();
}
});
httpReader
.execute("http://aa.domainlink.com:1324/example/API/v1/json.php?auth=example&a=ticket&uauth="
+ authCode + "&uid=" + uId + "&id=" + ticketId);

}

private void readSettings() {
InfoAdapter infoAdapter = new InfoAdapter(
getActivity(), ticketInfo);
final ListView listViewInfo = (ListView) rootView.findViewById(R.id.listViewInfo);
listViewInfo.setAdapter(infoAdapter);

listViewInfo
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parentView,
View childView, int position, long id) {
}
});
listViewInfo.setEmptyView(rootView.findViewById(R.id.empty));
}

private void fillSettings() {
String dueDate = getString(R.string.noDueDate);
ticketInfo = new ArrayList<Info>();
ticketInfo.add(new Info(getString(R.string.ticket_subject), ticket.getSubject() + ""));
ticketInfo.add(new Info(getString(R.string.ticket_relation), ticket.getRelation() + ""));
ticketInfo.add(new Info(getString(R.string.ticket_status), ticket.getStatus() + ""));
ticketInfo.add(new Info(getString(R.string.ticket_priority), ticket.getPriority() + ""));
ticketInfo.add(new Info(getString(R.string.ticket_created), ticket.getTicketCreate() + ""));
ticketInfo.add(new Info(getString(R.string.ticket_department), ticket.getDepartmentName() + ""));
ticketInfo.add(new Info(getString(R.string.ticket_user), ticket.getUser() + ""));
if (!ticket.getDueDate().equals("00-00-0000 00:00:00")) {
dueDate = ticket.getDueDate();
}
ticketInfo.add(new Info(getString(R.string.ticket_dueDate), dueDate));
readSettings();
}
}

Activity 通常需要的BaseActivity:

BaseActivity.java

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class BaseActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

// Add the Sample Fragment if there is one
Fragment sampleFragment = getSampleFragment();
if (sampleFragment != null) {
getFragmentManager().beginTransaction()
.replace(android.R.id.content, sampleFragment).commit();
}

}

//This method will override by child class. Then base class can get the fragment
protected Fragment getSampleFragment() {
return null;
}

}

张贴我在我的 fragment 中实现 Activity 方法的失败尝试我猜是行不通的,所以我没有张贴它们。

如果你们中的任何人可以提供帮助,非常感谢。

最佳答案

Android SDK 终于添加了 SwipeRefreshLayout。也许您可以将其添加到您的布局中? http://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

关于Android - 在选项卡式 fragment 中拉动刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22596536/

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