gpt4 book ai didi

android - fragment 加载后删除 Activity View

转载 作者:行者123 更新时间:2023-11-30 01:13:48 26 4
gpt4 key购买 nike

我有一个托管 fragment 的 Activity 。除了指示 fragment 正在加载的微调器外,该 Activity 基本上没有任何内容。该 fragment 依赖于稳定的互联网连接,因此微调器可见所需的时间长度本质上是动态的。

我想在 fragment 成功加载后删除 Activity 上的微调器。我尝试使用 isAdded() 方法,但该方法不起作用。任何帮助表示赞赏:

fragment :

public class LatestFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

private RecyclerView mRecyclerViewForLatestPolls;
private RecyclerView.Adapter mLatestAdapter;

private ArrayList<LatestPoll> mLatestPollsArray;

private DateFormat mDateFormat;
private Date mDate;
private String mCurrentDateString;

private Firebase mBaseRef;
private Firebase mPollRef;
private Firebase mUpdateRef;

private FragmentListener mFragmentListener;

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;


private OnFragmentInteractionListener mListener;

public LatestFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment LatestFragment.
*/
// TODO: Rename and change types and number of parameters
public static LatestFragment newInstance(String param1, String param2) {
LatestFragment fragment = new LatestFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDateFormat = new SimpleDateFormat("MM-dd-yyyy");
mDate = new Date();
mCurrentDateString = mDateFormat.format(mDate);
mBaseRef = FirebaseUtil.FIREBASE;
mPollRef = mBaseRef.child("Polls");
mUpdateRef = mPollRef.child(mCurrentDateString);


}

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

final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_latest, container, false);
getActivity().setTitle(R.string.latest_title);

mRecyclerViewForLatestPolls = (RecyclerView) rootView.findViewById(R.id.latest_RecyclerView);
mLatestPollsArray = new ArrayList<>();
mLatestAdapter = new MyAdapter(mLatestPollsArray);
LinearLayoutManager llm = new LinearLayoutManager(getActivity().getApplicationContext());
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerViewForLatestPolls.setLayoutManager(llm);
mRecyclerViewForLatestPolls.setItemAnimator(new SlideInLeftAnimator());
mRecyclerViewForLatestPolls.setAdapter(new AlphaInAnimationAdapter(mLatestAdapter));

mUpdateRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int numberOfPollsForDay = (int) dataSnapshot.getChildrenCount();
for (int i = 0; i < numberOfPollsForDay; i++) {
String latestPollQuestion = (String) dataSnapshot.child(String.valueOf(i + 1)).child("Poll_Question").getValue();
String pollImageURL = (String) dataSnapshot.child(String.valueOf(i + 1)).child("Image").getValue();
mLatestPollsArray.add(0, new LatestPoll(latestPollQuestion, pollImageURL));
mLatestAdapter.notifyDataSetChanged();
}
}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
});


// Inflate the layout for this fragment
return rootView;
}


// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}

// Force the parent activity to implement listener.
if (context instanceof FragmentListener) {
mFragmentListener = (FragmentListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
mFragmentListener = 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 OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {


private ArrayList<LatestPoll> mDataSet;
int lastPosition = -1;


// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
protected TextView pollQuestion;
protected ImageView pollImage;


public ViewHolder(View v) {
super(v);
pollQuestion = (TextView) v.findViewById(R.id.latest_item_question);
pollImage = (ImageView) v.findViewById(R.id.pollThumbNailImage);


}
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<LatestPoll> myDataset) {
mDataSet = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.latest_item, parent, false);
// set the view's size, margins, paddings and layout parameters
return new ViewHolder(v);
}

// Replace the contents of a view (invoked by the layout manager)

//The OutOfBoundsException is pointing here
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.v("ON_BIND", "ON_BINDVIEWHOLDER CALLED");
LatestPoll latestPoll = mDataSet.get(position);
holder.pollQuestion.setText(latestPoll.getQuestion());
Picasso.with(getActivity())
.load(latestPoll.getPollImage())
.fit()
.placeholder(R.drawable.loading_spinnter_white)
.into(holder.pollImage);
}




// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataSet.size();
}
}

private void onLoad() {
if (mFragmentListener != null) {
mFragmentListener.onFragmentLoaded();
}
}

public interface FragmentListener {
void onFragmentLoaded();
}


}

Activity XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/action_tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:titleTextColor="@color/white">

</android.support.v7.widget.Toolbar>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ProgressBar
android:id="@+id/pbHeaderProgress"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:progressDrawable="@drawable/loading_spinnter_white">
</ProgressBar>

<TextView
android:id="@+id/progress_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pbHeaderProgress"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="@string/loading_poll_data"
android:textColor="@color/white"
android:textSize="24sp" />
</RelativeLayout>

<FrameLayout
android:id="@+id/latest_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</FrameLayout>
</LinearLayout>

<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />

图片: enter image description here

最佳答案

在 fragment 中创建一个监听器接口(interface),

private MyFragmentListener mListener;

/**
* onLoad should be called when the fragment has loaded.
*/
private void onLoad() {
if (mListener != null) {
mListener.onFragmentLoaded();
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);

// Force the parent activity to implement listener.
if (context instanceof MyFragmentListener) {
mListener = (MyFragmentListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}


public interface MyFragmentListener {
void onFragmentLoaded();
}

然后在父 Activity 中,

public class MainActivity extends Activity implements MyFragment.MyFragmentListener{

@Override
public void onFragmentLoad() {
// HIDE the progressbar spinner.
}

参见 Communicating with Other Fragments了解更多信息。

关于android - fragment 加载后删除 Activity View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38163200/

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