ViewPager with Fragment Android ( Func<T,T,T> alternative in Java (Android)) - java

I am working with Tab Layout & View Pager which contains some views.
I've developed project for Xamarin Android and now I want to create with Android Studio.
I've implemented FragmentPagerAdapter same as former project, but problem is using Func in Fragment class.
My former class were like this :
public class SelectDateViewFragment : Android.Support.V4.App.Fragment
{
readonly Func<LayoutInflater, ViewGroup, Bundle, View> view;
public SelectDateViewFragment(Func<LayoutInflater, ViewGroup, Bundle, View> _view)
{
view = _view;
}
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
base.OnCreateView(inflater, container, savedInstanceState);
return view(inflater, container, savedInstanceState);
}
}
And I was able to create fragment and add to FragmentList like this :
viewPagerAdapter.addFragmentView((i, v, b) =>
{
var view = i.Inflate(Resource.Layout.SelectDateRecyclerViewLayout, v, false);
So, What would you recommend to be able to use same logic in my Activity ?
Thanks.

Okay problem's been solved by myself.
Basically I've implemented Func to Java with necessary parameters.
1) Creating an Interface
public interface Func<B,N,K, View> {
View invoke(B arg1,N arg2,K arg3);
}
2) Implementing Interface
class FuncImpl<B,N,K, View> implements Func<B,N,K, View> {
Func<B,N,K, View> function;
public FuncImpl(Func<B,N,K, View> function) {
this.function = function;
}
#Override
public View invoke(B arg1,N arg2,K arg3) {
return this.function.invoke(arg1, arg2, arg3);
}}
3) Using class in Fragment
public class SelectDateViewFragment extends Fragment {
Func<LayoutInflater,ViewGroup,Bundle,View> view;
public SelectDateViewFragment( Func<LayoutInflater,ViewGroup,Bundle,View> view){
this.view = view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return view.invoke(inflater,container,savedInstanceState);
}}
4) Setting ViewPager Adapter
selectDateAdapter = new SelectDateAdapter(getSupportFragmentManager(),charSequences);
selectDateAdapter.addFragmentView(new Func<LayoutInflater, ViewGroup, Bundle, View>() {
#Override
public View invoke(LayoutInflater arg1, ViewGroup arg2, Bundle arg3) {
View view = arg1.inflate(R.layout.layout_select_date_recyclerview,arg2,false);
return view;
}
});
viewPagerView.setAdapter(selectDateAdapter);
So this solution was uncommon but makes process quite simple.

Related

clickable card view simple without recycler view

I have a fragment and have linear layout that contains 2 simple card views.
How can I make card view clickable cardviews?
I have searched, but all topics are about cardviews in recycler view ... But I have a simple clickable cardview.
public class popFragment extends Fragment {
public popFragment()
{
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.popfragment,container,false);
return view;
}
}
You can simply assign id in your layout xml to each card view. then in oncreateView() of the fragment bind the view and use mycardView.setOnclickListener.... .
public class popFragment extends Fragment {
private CardView cardView1;
private CardView cardView2;
public popFragment()
{
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.popfragment,container,false);
cardView1 = view.findViewById(R.id.my_card_view_1);
cardView2 = view.findViewById(R.id.my_card_view_2);
cardView1.setOnClickListener(v->{
//set on click functions here
});
cardView2.setOnClickListener(v->{
//set on click functions here
});
return view;
}
}
do not forget to assign the corresponding ids in your layout.xml

BindView of Butterknife on fragments #onclick NullPointError

public class InputFragment extends Fragment {
public ContractMVP.start start;
void setPresenter(ContractMVP.start start){
this.start=start;
}
#BindView(R.id.Start_btn)Button btn;
#OnClick(R.id.Start_btn) void setBtn() {
start.start();
}
public static InputFragment newInstance() {
return new InputFragment();
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_input, container, false);
ButterKnife.bind(this,v);
return v;
}
I get NullpointException when ever I try to click the Button. When I debug it says start=null,
but when I use traditional findViewByid it works fine
Did you add both dependences
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor'com.jakewharton:butterknife-compiler:8.8.1'
Also check XML view Id and R.id.(yourId)

Use a view from a fragment in main activity

I'm pretty new in android development and I have some questions about communication between fragments and main activity.
I didn't success to solve my problem in my particular case. What I want to do is to modify the alpha of a view inside a fragment (something like a card) when scrolling from one fragment to the one with this view. I tried different things but I failed (null pointer exception), you can see some experiments in a comment. Here is my code and my architecture:
Fragment with the view:
public class VideoviewFragment extends BaseFragment {
/* public View card;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_videoview, container, false);
card = (View) rootview.findViewById(R.id.card_video);
return rootview;
}
public void setalphacard(float value){
card.setAlpha(value);
}*/
public static VideoviewFragment create() {
return new VideoviewFragment();
}
#Override
public int getLayoutResId() {
return R.layout.fragment_videoview;
}
#Override
public void inOnCreateView(View root, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
}
}
Base fragment:
public abstract class BaseFragment extends Fragment {
private View mRoot;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
mRoot = inflater.inflate(getLayoutResId(), container, false);
inOnCreateView(mRoot, container, savedInstanceState);
return mRoot;
}
#LayoutRes
public abstract int getLayoutResId();
public abstract void inOnCreateView(View root, #Nullable ViewGroup container,#Nullable Bundle savedInstanceState);
}
Main Activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Viewpager
final ViewPager viewPager = (ViewPager) findViewById(R.id.am_view_pager);
final MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
// tabview et viewpager
viewPager.setCurrentItem(1);
// Changement couleurs swipe
viewPager.setBackgroundColor(Color.rgb(135,206,250));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(position == 1) {
viewPager.setBackgroundColor((Integer) new ArgbEvaluator().evaluate(positionOffset, Color.rgb(135,206,250),Color.rgb(240,15,45)));
//videoviewFragment.setalphacard(positionOffset);
}
if(position == 0) {
viewPager.setBackgroundColor((Integer) new ArgbEvaluator().evaluate(positionOffset, Color.TRANSPARENT,Color.rgb(135,206,250)));
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
Adapter:
public class MainPagerAdapter extends FragmentPagerAdapter {
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return CameraFragment.create();
case 1:
return OptionFragment.create();
case 2:
return VideoviewFragment.create();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
XML VideoviewFragment
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout0"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/card_video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="70dp"
android:background="#drawable/card_background_video"/>
</RelativeLayout>
(I know there is no instance of my fragment in this activity...)
I found few similar topics on StackOverflow like this one Use view from fragment xml file, but I'm doing something wrong...
Thanks for your help!

Android: Accessing View object in a Fragment class

How do I access the View object created at the onCreateView method of a Fragment class from another method? I need to access the view in order to reference the Gridview control of my layout.
public class comments_frag_activity extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.comments_frag, container, false);
}
//other methods.....
//other methods .....
//other methods .....
public void printTest()
{
if(!commentsGallery.isEmpty())
{
//I cant access the view object 'android'
GridView list = (GridView)android.findViewById(R.id.commentinglist);
CommentsAdapter bA = new CommentsAdapter(ctx, R.layout.comments_frag, commentsGallery);
list.setAdapter(bA);
}
}
Save the view to a class variable.
private View view;
......
.....
#Override
onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(your layout)
}
Declare it globally instaed of local variable
Store view's references after onCreateView in onViewCreated:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
localGridView = (GridView) view.findViewById(R.id.commentinglist);
}
Try This
public class comments_frag_activity extends Fragment{
private View android;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
android = inflater.inflate(R.layout.comments_frag, container, false);
}
public void printTest()
{
if(!commentsGallery.isEmpty())
{
GridView list = (GridView)android.findViewById(R.id.commentinglist);
CommentsAdapter bA = new CommentsAdapter(ctx, R.layout.comments_frag, commentsGallery);
list.setAdapter(bA);
}
}
If you don't won't to store view globally (for some reason), you can always do something like
public void printTest()
{
if(!commentsGallery.isEmpty())
{
//I cant access the view object 'android'
GridView list = (GridView) getView().findViewById(R.id.commentinglist);
CommentsAdapter bA = new CommentsAdapter(ctx, R.layout.comments_frag, commentsGallery);
list.setAdapter(bA);
}
}
getView() will return the root layout inflated in onCreateView

onListItemClick in Fragment

I need use onListItemClick in my class, it's an extends Fragment, I don't how can I use it in my class, somebody knows how can I call it?
public class VisitaFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_visita, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Visitas visita = new Visitas();
List<Visita> visitas = visita.getVisitas();
final ListVisitaAdapter visitaAdapter = new ListVisitaAdapter(getActivity(), visitas);
ListView listVisitas = (ListView) getActivity().findViewById(R.id.lv_visita_emvisita);
listVisitas.setAdapter(visitaAdapter);
//-->> onListItemClick
}
}
listVisitas.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
Extend android.app.ListFragment instead of Fragment. It contains an onListItemClick() method that you can override.

Categories

Resources