将片段代码切换为活动代码

时间:2017-12-31 23:29:25

标签: java android android-fragments android-activity fragment

嗨,我是Android的初学者,我有一个包含历史片段的应用程序我试图将我的片段代码转换为活动代码!(我想在按钮点击后开始此活动)但我不知道如何这样做,请帮助我

这是我的片段:

public class HistoryFragment extends Fragment implements MainActivity.FragmentRefresh {

    ImageView ivSettings;
    private FragmentActivity mContext;
    private RecyclerView rvInsta;

    //DB
    private DBController dbcon;
    private ImageRecyclerAdaptor imageRecyclerAdaptor;

    public static HistoryFragment newInstance() {
        //Bundle args = new Bundle();
        //args.putString(ARG_PAGE, title);
        HistoryFragment fragment = new HistoryFragment();
        //fragment.setRetainInstance(true);
        //fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("Tag1","MoviesFrag");
    }

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

        View rootView = inflater.inflate(R.layout.fragment_history, container, false);
        mContext =getActivity();
        //DB
        dbcon = new DBController(mContext);

        rvInsta= (RecyclerView) rootView.findViewById(R.id.rvInstaImages);
        imageRecyclerAdaptor = new ImageRecyclerAdaptor(mContext);
        rvInsta.setAdapter(imageRecyclerAdaptor);
        rvInsta.setLayoutManager(new LinearLayoutManager(mContext));

        rvInsta.setHasFixedSize(true);
        rvInsta.setItemViewCacheSize(20);
        rvInsta.setDrawingCacheEnabled(true);
        rvInsta.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        //rvInsta.s/

        imageRecyclerAdaptor.notifyDataSetChanged();
        return rootView;
    }

    @Override public void refresh() {
        if (imageRecyclerAdaptor!=null) {
            imageRecyclerAdaptor.onRefreshh();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends AppCompatActivity implements FragmentRefresh{
ImageView ivSettings;
private RecyclerView rvInsta;

//DB
private DBController dbcon;
private ImageRecyclerAdaptor imageRecyclerAdaptor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_history);

    dbcon = new DBController(this);

    rvInsta= (RecyclerView) findViewById(R.id.rvInstaImages);
    imageRecyclerAdaptor = new ImageRecyclerAdaptor(this);
    rvInsta.setLayoutManager(new LinearLayoutManager(this));
    rvInsta.setAdapter(imageRecyclerAdaptor);


    rvInsta.setHasFixedSize(true);
    rvInsta.setItemViewCacheSize(20);
    rvInsta.setDrawingCacheEnabled(true);
    rvInsta.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    imageRecyclerAdaptor.notifyDataSetChanged();

}

@Override public void refresh() {
    if (imageRecyclerAdaptor!= null) {
        imageRecyclerAdaptor.onRefreshh();
    }
}

}