Guys I am just a student, creating a movieapp as a project and I need some help, iI've been looking online and followed all the step and yet I am not able to click the imagebutton in the fragment. Please help me and teach me what's wrong.
im currently just doing imgdp which you can see the id of imgdp and the page i want to direct to is Deadpool2.class.
This is my fragment code:
public class fragment4 extends Fragment {
ImageButton imgbutton;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public fragment4() {
// Required empty public constructor
}
public static fragment4 newInstance(String param1, String param2) {
fragment4 fragment = new fragment4();
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);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment4, container, false);
imgbutton = (ImageButton) view.findViewById(R.id.imgdp);
imgbutton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), Deadpool2.class));
}
});
return view;
}
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");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
My XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment4">
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Home$PlaceholderFragment">
<TextView
android:id="#+id/textView13"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Action"
android:textSize="18sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.063"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<ImageButton
android:id="#+id/imgdp"
android:layout_width="118dp"
android:layout_height="155dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="9dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="#drawable/deadpool"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.028"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.983" />
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="118dp"
android:layout_height="155dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="#drawable/avengersinfinitywar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.971"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.52" />
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="118dp"
android:layout_height="155dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="#drawable/avengersinfinitywar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.061" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="118dp"
android:layout_height="155dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="#drawable/thor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.983" />
<ImageButton
android:id="#+id/imageButton8"
android:layout_width="118dp"
android:layout_height="155dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="#drawable/jurassic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.028"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.522" />
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="118dp"
android:layout_height="155dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="#drawable/thor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.971"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.061" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="118dp"
android:layout_height="155dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="#drawable/incredibles"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.028"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.063" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="118dp"
android:layout_height="155dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="#drawable/kingsman"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.971"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.983" />
<ImageButton
android:id="#+id/imageButton9"
android:layout_width="118dp"
android:layout_height="155dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="#drawable/deadpool"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.52" />
<TextView
android:id="#+id/textView14"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Adventure"
android:textSize="18sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.065"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.347" />
<TextView
android:id="#+id/textView15"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Comdey"
android:textSize="18sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.065"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.691" />
</android.support.constraint.ConstraintLayout>
This is My Home activity
public class Home extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
private Home.SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mToolbar = (Toolbar) findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mSectionsPagerAdapter = new Home.SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch(getArguments().getInt(ARG_SECTION_NUMBER))
{
case 1:
rootView = inflater.inflate(R.layout.fragment_fragment1, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_fragment2, container, false);
break;
case 3:
rootView = inflater.inflate(R.layout.fragment_fragment3, container, false);
break;
case 4:
rootView = inflater.inflate(R.layout.fragment_fragment4, container, false);
break;
}
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position){
switch (position){
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
case 4:
return "SECTION 4";
}
return null;
}
}
}
What's the error you get? Try to make your question clearer to get proper help.
Related
I created a recyclerView that has several items but the problem is when I want to look for the address inside the map
When I scroll through the map, the recycler view shifts and the map loses scrollability.
enter link description here
//Recycler View Adapter Class
public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.Holder> {
private List<FakeAddressList> objectList;
public AddressAdapter(List<FakeAddressList> list) {
this.objectList = list;
}
#NonNull
#Override
public Holder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_address, parent, false);
return new Holder(view);
}
#Override
public void onBindViewHolder(#NonNull Holder holder, int position) {
holder.bindAddressList(objectList.get(position));
holder.imgClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
objectList.remove(position);
notifyDataSetChanged();
}
});
holder.imgEditLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AddAddressFragment addressFragment = new AddAddressFragment();
FragmentTransaction ft = fm.beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("Title", objectList.get(position).getTitle());
bundle.putString("Address", objectList.get(position).getAddress());
bundle.putString("Phone", objectList.get(position).getPhoneNumber());
addressFragment.setArguments(bundle);
ft.replace(R.id.content_view, addressFragment).addToBackStack(null).commit();
}
});
holder.FooterLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AddDataFragment addDataFragment = new AddDataFragment();
FragmentTransaction ft = fm.beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("Title", objectList.get(position).getAddress());
bundle.putString("Address", objectList.get(position).getAddress());
bundle.putString("Phone", objectList.get(position).getPhoneNumber());
addDataFragment.setArguments(bundle);
ft.replace(R.id.content_view, addDataFragment).addToBackStack(null).commit();
}
});
}
#Override
public int getItemCount() {
return objectList.size();
}
public class Holder extends RecyclerView.ViewHolder {
public TextView txtTitle;
public TextView txtSelect;
public TextView txtAddress;
public TextView txtPhone;
public ImageView imgClose;
public ImageView imgEditLocation;
public ConstraintLayout FooterLayout;
public MapView map;
public Holder(#NonNull View itemView) {
super(itemView);
txtTitle = itemView.findViewById(R.id.txtTitle);
txtAddress = itemView.findViewById(R.id.txtAddress);
txtPhone = itemView.findViewById(R.id.txtPhoneNumber);
txtSelect = itemView.findViewById(R.id.txtSelect);
imgClose = itemView.findViewById(R.id.imgClose);
imgEditLocation = itemView.findViewById(R.id.imgEditLocation);
FooterLayout = itemView.findViewById(R.id.FooterLayout);
map = itemView.findViewById(R.id.mapView);
txtTitle.setTypeface(Font_shabnam);
txtAddress.setTypeface(Font_shabnam);
txtPhone.setTypeface(Font_shabnam);
txtSelect.setTypeface(Font_shabnam);
}
public void bindAddressList(final FakeAddressList fakeAddressList) {
txtTitle.setText(fakeAddressList.getTitle());
txtAddress.setText(fakeAddressList.getAddress());
txtPhone.setText(fakeAddressList.getPhoneNumber());
if (fakeAddressList.getLng() != null) {
double lng = Double.valueOf(fakeAddressList.getLng());
double lat = Double.valueOf(fakeAddressList.getLat());
addUserMarker(new LngLat(lng, lat));
map.setFocalPointPosition(
new LngLat(Double.valueOf(fakeAddressList.getLng()), Double.valueOf(fakeAddressList.getLat())), 0.25f);
} else {
LngLat focalPoint;
focalPoint = new LngLat(51.33800, 35.69997);
map.setFocalPointPosition(focalPoint, 0f);
}
map.setZoom(15, 0.25f);
map.getLayers().add(NeshanServices.createBaseMap(NeshanMapStyle.STANDARD_DAY));
}
// This method gets a LngLat as input and adds a marker on that position
private void addUserMarker(LngLat loc) {
Marker marker;
VectorElementLayer userMarkerLayer;
userMarkerLayer = NeshanServices.createVectorElementLayer();
map.getLayers().add(userMarkerLayer);
// Creating marker style. We should use an object of type MarkerStyleCreator, set all features on it
// and then call buildStyle method on it. This method returns an object of type MarkerStyle
MarkerStyleCreator markStCr = new MarkerStyleCreator();
markStCr.setSize(20f);
// markStCr.setBitmap(BitmapUtils.createBitmapFromAndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_marker)));
markStCr.setBitmap(BitmapUtils.createBitmapFromAndroidBitmap(BitmapFactory.decodeResource(itemView.getResources(), R.drawable.ic_cherry)));
MarkerStyle markSt = markStCr.buildStyle();
// Creating user marker
marker = new Marker(loc, markSt);
// Clearing userMarkerLayer
// userMarkerLayer.clear();
// Adding user marker to userMarkerLayer, or showing marker on map!
userMarkerLayer.add(marker);
}
}
}
Recycler View Xml Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/HeaderLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:background="#drawable/_orange_shape_b_r_l"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<ImageView
android:id="#+id/imgClose"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_close"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imgEditLocation"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_edit_location"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="تهرانپارس"
android:textSize="18dp"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="#+id/imgEditLocation"
app:layout_constraintEnd_toStartOf="#+id/imgEditLocation"
app:layout_constraintStart_toEndOf="#+id/imgClose"
app:layout_constraintTop_toTopOf="#+id/imgClose" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/MiddleLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/HeaderLayout"
app:layout_constraintVertical_bias="0.0">
<org.neshan.ui.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imgAddress"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_address"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/mapView"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/txtAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="خیابان فلانی کوچه 1"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="#+id/imgAddress"
app:layout_constraintTop_toTopOf="#+id/imgAddress" />
<ImageView
android:id="#+id/imgPhone"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_phone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/imgAddress"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/txtPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0916123456"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="#+id/imgPhone"
app:layout_constraintTop_toTopOf="#+id/imgPhone" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/FooterLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/_black_shape_t_r_l"
android:padding="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/MiddleLayout"
app:layout_constraintVertical_bias="0.0">
<TextView
android:id="#+id/txtSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="انتخاب"
android:textColor="#color/white"
android:textSize="18dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
//Fragment that Load Recyclerview
public class RegisterAddressFragment extends Fragment
implements View.OnClickListener {
private RecyclerView recycler_SelectAddress;
private Button btnNewAddress;
private ProgressBar progressBar;
private ConstraintLayout MainLayout;
private Bundle bundle;
private ArrayList<FakeAddressList> list = new ArrayList<>();
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
this.bundle = getArguments();
if (bundle != null) {
list.add(new FakeAddressList(bundle.getString("Title"),
bundle.getString("Address"),
bundle.getString("Phone"),
bundle.getString("Lng"),
bundle.getString("Lat")));
}
}
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_register_address, container, false);
init(v);
initRecyclerView();
return v;
}
private void init(View v) {
recycler_SelectAddress = v.findViewById(R.id.recycler_SelectAddress);
btnNewAddress = v.findViewById(R.id.btnNewAddress);
btnNewAddress.setOnClickListener(this);
progressBar = v.findViewById(R.id.progressbar);
MainLayout = v.findViewById(R.id.MainLayout);
Thread thread = new Thread() {
#Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
// Do some stuff
progressBar.setVisibility(View.GONE);
MainLayout.setVisibility(View.VISIBLE);
}
});
}
};
thread.start(); //start the thread
}
private void initRecyclerView() {
list.add(new FakeAddressList("اصفهان", "شاهین شهر خیابان نوشین پلاک 1", "987654321"));
list.add(new FakeAddressList("اهواز", "کیانپارس کوچه بنی هاشم پلاک 33", "123456789"));
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(RecyclerView.VERTICAL);
AddressAdapter addressAdapter = new AddressAdapter(list);
recycler_SelectAddress.setLayoutManager(layoutManager);
recycler_SelectAddress.setAdapter(addressAdapter);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnNewAddress:
AddAddressFragment addressFragment = new AddAddressFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_view, addressFragment).addToBackStack(null).commit();
break;
}
}
}
Fragment Xml File
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/tusi"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="#color/red2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/MainLayout"
tools:visibility="visible"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_SelectAddress"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toTopOf="#+id/btnNewAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btnNewAddress"
android:layout_width="match_parent"
android:layout_height="0dp"
android:backgroundTint="#color/navy_blue"
android:insetLeft="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
android:insetBottom="0dp"
android:text="آدرس جدید"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I found the solution :
I added below code in the adapter of the recyclerview
map.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
itemView.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
To summarize, I'm building a social media app that displays:
A Newsfeed that shows posts from all user profiles in a given account
A Timeline that shows posts from a specific user profile in the account
I've built a custom BaseAdapter to populate a custom cell within each ListView. The Newsfeed ListView (that populates posts from all users on the account) is populating correctly.
The Timeline ListView (that populates posts from one profile on the account) is not showing. I've set breakpoints to ensure that my ArrayList is not null or empty when populating the Timeline ListView. In addition, breakpoints verify that my custom adapter is, in fact, pulling data from the ArrayList and inflating cells. However, the ListView is simply not visible.
Here is the layout file for my Newsfeed fragment (that is working correctly):
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorSkyPrimary">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:elevation="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="#+id/input_post"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:backgroundTint="#color/colorSkyPrimary"
android:ems="10"
android:gravity="start|top"
android:hint="#string/what_s_going_on"
android:importantForAutofill="no"
android:inputType="textMultiLine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:targetApi="o" />
<ImageButton
android:id="#+id/button_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:backgroundTint="#android:color/white"
android:contentDescription="#string/camera_button"
app:layout_constraintBottom_toBottomOf="#+id/input_post"
app:layout_constraintEnd_toEndOf="#+id/input_post"
app:srcCompat="#drawable/camera_icon" />
<Button
android:id="#+id/button_cancel"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginTop="8dp"
android:text="#android:string/cancel"
android:textColor="#color/colorGrassPrimary"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/post_image" />
<Button
android:id="#+id/button_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="48dp"
android:backgroundTint="#color/colorGrassPrimary"
android:text="#string/update"
android:textColor="#color/colorButtonText"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/post_image" />
<ImageView
android:id="#+id/post_image"
android:layout_width="0dp"
android:layout_height="300dp"
android:contentDescription="#string/post_image"
android:scaleType="fitCenter"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/input_post"
tools:srcCompat="#tools:sample/avatars" />
</android.support.constraint.ConstraintLayout>
<ListView
android:id="#+id/list_newsfeed"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/picker_image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout"
app:layout_constraintVertical_bias="0.0" />
<android.support.constraint.ConstraintLayout
android:id="#+id/picker_image"
android:layout_width="0dp"
android:layout_height="75dp"
android:background="#android:color/white"
android:elevation="16dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<android.support.constraint.Guideline
android:id="#+id/guideline14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<ImageButton
android:id="#+id/button_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="8dp"
android:background="#android:color/transparent"
android:contentDescription="#string/camera_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/camera_icon_large" />
<ImageButton
android:id="#+id/button_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#android:color/transparent"
android:contentDescription="#string/gallery_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.375"
app:srcCompat="#drawable/gallery_icon" />
</android.support.constraint.ConstraintLayout>
Here is the layout file for my Profile fragment (in which the ListView is not showing at runtime)
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorSkyPrimary">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:elevation="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_photo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:src="#drawable/male_icon_large"
app:civ_border_color="#android:color/transparent"
app:civ_border_width="2dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/display_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="#string/brelynn_mack"
android:textAlignment="viewStart"
android:textColor="#color/colorTextDark"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#+id/button_delete_profile"
app:layout_constraintStart_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toTopOf="#+id/profile_photo" />
<TextView
android:id="#+id/display_timestamp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="#string/may_14_2019_9_59_pm"
android:textAlignment="viewStart"
android:textColor="#color/colorTextLight"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/display_name" />
<TextView
android:id="#+id/display_last_location"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="#string/_8741_grouse_run_lane_28314"
android:textAlignment="viewStart"
android:textColor="#color/colorTextPrimary"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/display_timestamp" />
<ImageButton
android:id="#+id/button_delete_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="#android:color/white"
android:contentDescription="#string/trash_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/trash_icon" />
<ImageButton
android:id="#+id/button_photo"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="16dp"
android:background="#android:color/white"
android:contentDescription="#string/camera_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/profile_photo"
app:srcCompat="#drawable/camera_icon" />
<ImageButton
android:id="#+id/button_family"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:background="#android:color/white"
android:contentDescription="#string/family_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#drawable/family_icon_small" />
<ImageButton
android:id="#+id/button_gallery"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginEnd="8dp"
android:background="#android:color/transparent"
android:contentDescription="#string/gallery_button"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/profile_photo"
app:srcCompat="#drawable/gallery_icon" />
</android.support.constraint.ConstraintLayout>
<ListView
android:id="#+id/list_posts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout4" />
Here is my custom adapter:
public class NewsfeedAdapter extends BaseAdapter {
// Class properties
private static final String TAG = "NewsfeedAdapter";
public static final String EXTRA_POSTS = "extra_posts";
public static final String EXTRA_POSITION = "extra_position";
private final Context context;
ArrayList<Post> posts;
Account account;
// Constructor
public NewsfeedAdapter(Context context, ArrayList<Post> posts, Account account) {
this.context = context;
this.posts = posts;
this.account = account;
}
// System generated methods
#Override
public int getCount() {
return posts.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public Object getItem(int position) {
return posts.get(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Post post = posts.get(position);
if(convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(R.layout.cell_newsfeed, null);
}
TextView profileNameDisplay = convertView.findViewById(R.id.display_profile_name);
String name = post.getPosterName() + " " + account.getFamilyName();
profileNameDisplay.setText(name);
TextView timestampDisplay = convertView.findViewById(R.id.display_timestamp);
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM dd, yyyy # hh:mm a", Locale.getDefault());
String timestamp = dateFormat.format(post.getTimeStamp());
timestampDisplay.setText(timestamp);
TextView postMessageDisplay = convertView.findViewById(R.id.display_post_message);
postMessageDisplay.setText(post.getPostMessage());
if (post.getHasImage()) {
AccountUtils.loadProfilePhoto(context, convertView, post.getPosterId());
}
else {
ImageView postImage = convertView.findViewById(R.id.display_post_image);
postImage.setVisibility(View.GONE);
}
PostUtils.loadPostImage(convertView, post.getPostId());
ImageButton deleteButton = convertView.findViewById(R.id.button_delete_post);
ImageButton editButton = convertView.findViewById(R.id.button_edit_post);
toggleButtons(post, editButton, deleteButton);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setTitle(context.getString(R.string.delete_post));
alertBuilder.setMessage(context.getString(R.string.delete_post_message));
alertBuilder.setPositiveButton(context.getString(R.string.delete), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
posts.remove(position);
PostUtils.deletePost(context, post.getPostId(), post.getPosterId());
notifyDataSetChanged();
PostUtils.listenForNews(context);
}
});
alertBuilder.setNegativeButton(context.getString(R.string.cancel), null);
AlertDialog alert = alertBuilder.create();
alert.show();
}
});
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent editIntent = new Intent(context, EditPostActivity.class);
editIntent.putExtra(EXTRA_POSTS, posts);
editIntent.putExtra(EXTRA_POSITION, position);
context.startActivity(editIntent);
}
});
return convertView;
}
// Custom methods
private void toggleButtons(Post post, ImageButton editButton, ImageButton deleteButton) {
long twoMinutes = System.currentTimeMillis() - (2 * 60 * 1000);
long fiveMinutes = System.currentTimeMillis() - (5 * 60 * 1000);
if (post.getTimeStamp().getTime() < fiveMinutes) {
editButton.setVisibility(View.GONE);
}
else {
editButton.setVisibility(View.VISIBLE);
}
if (post.getTimeStamp().getTime() < twoMinutes) {
deleteButton.setVisibility(View.GONE);
}
else {
deleteButton.setVisibility(View.VISIBLE);
}
}
Here are the lifecycle methods from the Newsfeed fragment that load my data and set the adapter:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_newsfeed, container, false);
PostUtils.listenForNews(getActivity());
mPosts = PostUtils.loadNewsfeed(getActivity());
mNewsfeed = view.findViewById(R.id.list_newsfeed);
mImagePicker = view.findViewById(R.id.picker_image);
mPhotoView = view.findViewById(R.id.post_image);
AccountUtils.listenForUpdates(getActivity());
setClickListener(view);
setFocusListener(view);
return view;
}
#Override
public void onResume() {
super.onResume();
mPosts = PostUtils.loadNewsfeed(getActivity());
Account account = AccountUtils.loadAccount(getActivity());
mNewsfeedAdapter = new NewsfeedAdapter(getActivity(), mPosts, account);
mNewsfeed.setAdapter(mNewsfeedAdapter);
}
Here are the lifecycle methods from my Profile fragment where the same functionality should work for a slightly different set of data:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
try {
if(getActivity().getIntent() != null && getActivity().getIntent().getAction().equals(FamilyProfileFragment.ACTION_EDIT_PROFILE)) {
mEditingSelf = false;
mIsParent = true;
mProfile = (Profile) getActivity().getIntent().getSerializableExtra(FamilyProfileFragment.EXTRA_PROFILE);
String selectedName = mProfile.getFirstName();
String loadedName = AccountUtils.loadProfile(getActivity()).getFirstName();
if(selectedName.equals(loadedName)) {
mEditingSelf = true;
}
}
else {
mEditingSelf = true;
mProfile = AccountUtils.loadProfile(getActivity());
if(mProfile instanceof Parent) {
mIsParent = true;
}
else {
mIsParent = false;
}
}
}
catch (Exception e) {
e.printStackTrace();
mEditingSelf = true;
mProfile = AccountUtils.loadProfile(getActivity());
if(mProfile instanceof Parent) {
mIsParent = true;
}
else {
mIsParent = false;
}
}
mAccount = AccountUtils.loadAccount(getActivity());
AccountUtils.loadProfilePhoto(getActivity(), view, mProfile.getProfileId());
mPhotoView = view.findViewById(R.id.profile_photo);
PostUtils.listenForTimeline(getActivity(), mProfile);
mPosts = PostUtils.loadTimeline(getActivity());
mTimeline = view.findViewById(R.id.list_posts);
setClickListener(view);
setTextDisplay(view);
populateProfile(view);
return view;
}
#Override
public void onResume() {
super.onResume();
mPosts = PostUtils.loadTimeline(getActivity());
mNewsfeedAdapter = new NewsfeedAdapter(getActivity(), mPosts, mAccount);
mTimeline.setAdapter(mNewsfeedAdapter);
}
My constraints were set up incorrectly in the Profile fragment. Updated the XML to the following:
<ListView
android:id="#+id/list_newsfeed"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout4" />
I have a simple main java "GameMode" that will show 3 tabs fragments, one of them is "HkmjSetting" (will be shown below), now when I run the app, it works just fine, but any click on the button has no response. No red lines were found in android studio..
GameMode java:
public class GameMode extends AppCompatActivity {
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_game_mode, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (getArguments().getInt(ARG_SECTION_NUMBER)==1) {
View rootView = inflater.inflate(R.layout.fragment_hkmj_setting, container, false);
return rootView;
} else if (getArguments().getInt(ARG_SECTION_NUMBER)==2) {
View rootView = inflater.inflate(R.layout.fragment_quickmj_setting, container, false);
return rootView;
} else {
View rootView = inflater.inflate(R.layout.fragment_hkmj_setting, container, false);
// TextView textView = (TextView) rootView.findViewById(R.id.section_label);
//textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
}
HkmjSetting java:
public class HkmjSetting extends Fragment {
Button btn_next;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_hkmj_setting, container, false);
btn_next = (Button) view.findViewById(R.id.btn_next);
btn_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* Intent intent = new Intent(v.getContext(), MainActivity.class);
startActivity(intent);*/
Toast.makeText(getActivity(), "TESTING", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
Run results no error, but nothing happen when clicked on the "btn_next", this may be a silly question to many of you, but please help...Thanks!
2018-05-08 Updating Xml layout codes as comments suggest, I simplified a bit for easier reading
fragment_hkmj_setting.xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GameSetting">
<!-- TODO: Update blank fragment layout -->
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HkmjSetting">
<LinearLayout
android:id="#+id/edtxList_6to10"
android:layout_width="57dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.72"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.602">
<TextView
android:id="#+id/tv_10Fan"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginBottom="0dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/tv_10Fan"
android:textColor="#color/ColorDropiii"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.024"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.982" />
</LinearLayout>
<ImageButton
android:id="#+id/ibtn_setting2"
android:layout_width="58dp"
android:layout_height="52dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="4dp"
android:layout_marginTop="8dp"
android:contentDescription="#string/tv_Setting"
android:src="#drawable/hkmj"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/tv_pageTitle"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</LinearLayout>
<Button
android:id="#+id/btn_next"
android:layout_width="139dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#color/ColorDropii"
android:text="#string/btn_next"
android:textColor="#color/ColorDropiv"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.876" />
</android.support.constraint.ConstraintLayout>
</FrameLayout>
activity_game_mode.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".GameMode">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="#string/app_name">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:id="#+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="香港麻雀" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跑馬仔" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="台灣麻雀" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Change the getItem() method body to if (position == 2) { return new HkmjSetting(); } else { return PlaceholderFragment.newInstance(position + 1); }
as suggested by #Mike M.
Thanks! it works!
I have been struggling a few days with this problem.
It is really strange.
I have a fragment with the following structure.
ScrollView
ConstraintLayout
TextView1
TwoWayView1
TextView2
TwoWayView2
The TwoWayView (RecyclerView library) is being populated with the items,
I added a onItemClickListener to test if the items is indeed there.
So when I tap on an empty space in the area where the item should be. It gives me the correct position and name of that item. But the item is not visible.
I really hope someone can help me.
Here is my code.
fragment_events
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:context="EventsFragment">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvHeaderSocialEvents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text="Social Events"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintHorizontal_bias="0.033"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<org.lucasr.twowayview.widget.TwoWayView
android:id="#+id/lvSocialEvents"
style="#style/TwoWayView"
android:layout_width="0dp"
android:layout_height="260dp"
android:layout_marginTop="8dp"
android:clipChildren="true"
android:clipToPadding="false"
android:drawSelectorOnTop="false"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvHeaderSocialEvents"
tools:context=".activities.MenuGuillotineActivity"
tools:layout_conversion_absoluteHeight="80dp"
tools:layout_conversion_absoluteWidth="395dp"
tools:layout_conversion_absoluteX="8dp"
tools:layout_conversion_absoluteY="68dp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="846dp"
android:text="temp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="175dp" />
<TextView
android:id="#+id/tvHeaderSportEvents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text="Sport Events"
android:textSize="24sp"
app:layout_constraintHorizontal_bias="0.027"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/lvSocialEvents" />
<org.lucasr.twowayview.widget.TwoWayView
android:id="#+id/lvSportEvents"
style="#style/TwoWayView"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_marginTop="8dp"
android:drawSelectorOnTop="false"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvHeaderSportEvents"
tools:LayoutManager="TwoWayLayoutManager"
tools:context=".activities.MenuGuillotineActivity"
tools:layout_conversion_absoluteHeight="80dp"
tools:layout_conversion_absoluteWidth="395dp"
tools:layout_conversion_absoluteX="8dp"
tools:layout_conversion_absoluteY="68dp" />
<TextView
android:id="#+id/tvHeaderOfficialEvents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text="Official Events"
android:textSize="24sp"
app:layout_constraintHorizontal_bias="0.025"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/lvSportEvents" />
<org.lucasr.twowayview.widget.TwoWayView
android:id="#+id/lvOfficialEvents"
style="#style/TwoWayView"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_marginTop="8dp"
android:drawSelectorOnTop="false"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvHeaderOfficialEvents"
tools:LayoutManager="TwoWayLayoutManager"
tools:context=".activities.MenuGuillotineActivity"
tools:layout_conversion_absoluteHeight="80dp"
tools:layout_conversion_absoluteWidth="395dp"
tools:layout_conversion_absoluteX="8dp"
tools:layout_conversion_absoluteY="68dp" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
EventsFragment
public class EventsFragment extends Fragment {
ArrayList<EventClass> eventClassArrayList;
RecyclerView.Adapter eventClassEventsAdapter;
TwoWayView lvSocialEvents;
View masterView;
TwoWayView lvSportEvents;
TwoWayView lvOfficialEvents;
StudentClass studentClass;
String studRank;
Context context;
public EventsFragment() {
// Required empty public constructor
}
private void createHeadings(View view) {
TextView lvHeaderSocialEvents = (TextView) view.findViewById(R.id.tvHeaderSocialEvents);
lvHeaderSocialEvents.setTypeface(EasyFonts.caviarDreamsBold(context));
TextView lvHeaderSportEvents = (TextView) view.findViewById(R.id.tvHeaderSportEvents);
lvHeaderSportEvents.setTypeface(EasyFonts.caviarDreamsBold(context));
TextView lvHeaderOfficialEvents = (TextView) view.findViewById(R.id.tvHeaderOfficialEvents);
lvHeaderOfficialEvents.setTypeface(EasyFonts.caviarDreamsBold(context));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_events, container, false);
masterView = view;
// Get Application Context
context = getContext().getApplicationContext();
Fresco.initialize(context);
// Assign components
lvSocialEvents = (TwoWayView) view.findViewById(R.id.lvSocialEvents);
lvSocialEvents.setLayoutManager(new ListLayoutManager(context, TwoWayLayoutManager.Orientation.HORIZONTAL));
lvSocialEvents.setHasFixedSize(true);
lvSportEvents = (TwoWayView) view.findViewById(R.id.lvSportEvents);
lvSportEvents.setLayoutManager(new ListLayoutManager(context, TwoWayLayoutManager.Orientation.HORIZONTAL));
lvSportEvents.setHasFixedSize(true);
lvOfficialEvents = (TwoWayView) view.findViewById(R.id.lvOfficialEvents);
lvOfficialEvents.setLayoutManager(new ListLayoutManager(context, TwoWayLayoutManager.Orientation.HORIZONTAL));
lvOfficialEvents.setHasFixedSize(true);
createHeadings(view);
// Get global data
SharedPreferences global = context.getSharedPreferences("globalData", MODE_PRIVATE);
if (global.contains("student")) {
//Get json from SP and decode json to EmployeeClass.
SharedPreferences shared = context.getSharedPreferences("studentData", MODE_PRIVATE);
Gson gson = new Gson();
String json = shared.getString("studentObject", "");
studentClass = gson.fromJson(json, StudentClass.class);
studRank = studentClass.rank;
} else {
studRank = "0";
}
// Get all the events from the Web server
StringRequest stringRequest = new StringRequest(Request.Method.POST, getResources().getString(R.string.url_event), new Response.Listener<String>() {
#Override
public void onResponse(String output) {
if (!output.isEmpty()) {
ArrayList<CommunicationClass> data = new JsonConverter<CommunicationClass>().toArrayList(output, CommunicationClass.class);
CommunicationClass comm = data.get(0);
if (comm.code.equals("0")) {
// Create list for Events
eventClassArrayList = new JsonConverter<EventClass>().toArrayList(comm.info, EventClass.class);
// Create and add events to EventsAdapter and add to TwoWayView
eventClassEventsAdapter = new EventsAdapter(context,eventClassArrayList);
lvSocialEvents.setAdapter(eventClassEventsAdapter);
lvSportEvents.setAdapter(eventClassEventsAdapter);
lvOfficialEvents.setAdapter(eventClassEventsAdapter);
eventClassEventsAdapter.notifyDataSetChanged();
} else {
ErrorHandling.makeText(context,comm.code, false).show();
}
} else {
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
ErrorHandling.makeText(context, volleyError).showVolley();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
//POST data for PHP Script
HashMap<String, String> postDataEvents = new HashMap<String, String>();
postDataEvents.put("event", "true");
if (studRank.equals("0")) {
postDataEvents.put("token", "public");
} else {
postDataEvents.put("token", studentClass.token);
}
return postDataEvents;
}
};
MySingleton.getInstance(context).addToRequestQueue(stringRequest);
com.example.howldevelopment.bcofficial.interfaces.ItemClickSupport itemClickSupport = com.example.howldevelopment.bcofficial.interfaces.ItemClickSupport.addTo(lvSocialEvents);
itemClickSupport.setOnItemClickListener(new com.example.howldevelopment.bcofficial.interfaces.ItemClickSupport.OnItemClickListener() {
#Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
Toast.makeText(context, eventClassArrayList.get(position).name, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
EventsAdapter
public class EventsAdapter extends RecyclerView.Adapter<EventsAdapter.EventViewHolder> {
private Context context;
private ArrayList<EventClass> events;
private LayoutInflater mInflater;
public EventsAdapter(Context context,ArrayList<EventClass> events) {
mInflater = LayoutInflater.from(context);
this.context = context;
this.events = events;
}
#Override
public EventViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.event_item_1, parent, false);
EventViewHolder holder = new EventViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(EventViewHolder holder, int position) {
Toast.makeText(context, events.get(position).name, Toast.LENGTH_SHORT).show();
holder.tvName.setText(events.get(position).name);
holder.tvDate.setText(events.get(position).date);
holder.tvCost.setText(events.get(position).cost);
holder.tvInterested.setText(events.get(position).interested);
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public int getItemCount() {
return events.size();
}
class EventViewHolder extends RecyclerView.ViewHolder {
TextView tvName;
TextView tvDate;
TextView tvCost;
TextView tvInterested;
ImageView imgPoster;
public EventViewHolder(View itemView) {
super(itemView);
tvName = (TextView) itemView.findViewById(R.id.tvEventName);
tvDate = (TextView) itemView.findViewById(R.id.tvEventDate);
tvCost = (TextView) itemView.findViewById(R.id.tvEventPrice);
tvInterested = (TextView) itemView.findViewById(R.id.tvEventInterested);
}
}
}
It did work a few hours ago. But after added more components and the required code in the EventsAdapter. It stopped working. And try to go back to the working build, But no success
I also get E/RecyclerView: No adapter attached; skipping layout error
Android Studio showed that my font color was the greyish color, while in real life it was actually white (or my Accent Color).
I just changed the color of the text font and everything was working perfectly.
I am developing an application with accessing gesture of android. I am not able to swipe over the layout where some texts are visible,but able to swipe in blank spaces of activity.
my java code
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.tomcast.libin.anxiety.R;
import com.tomcast.libin.anxiety.models.Questions;
import com.tomcast.libin.anxiety.utils.OnSwipeTouchListener;
import com.tomcast.libin.anxiety.utils.SlideAnimationUtil;
import java.util.ArrayList;
public class DFragment 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";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private ArrayList<Questions> questionlist = new ArrayList<Questions>();
private static int START_POSITION=0;
private static int FINAL_POSITION=6;
private static int CURRENT_POSTION =0;
ConstraintLayout llQuestion;
TextView question;
ImageView iconA;
ImageView iconB;
ImageView iconC ;
ImageView iconD;
float x1,y1,x2,y2,t1,t2;
`enter code here`int CLICK_DURATION = 10;
private OnFragmentInteractionListener mListener;
public DFragment() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static Fragment newInstance(String param1, String param2) {
Fragment fragment = new Fragment();
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);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.sliding_question_layout, container, false);
init( view);
return view ;
}
void init(final View view){
llQuestion=(ConstraintLayout) view.findViewById(R.id.constraintLayout);
iconA= view.findViewById(R.id.ic_option_a);
iconB= view.findViewById(R.id.ic_option_b);
iconC= view.findViewById(R.id.ic_option_c);
iconD= view.findViewById(R.id.ic_option_d);
Questions q0 = new Questions();
q0.setQuestion("Feeling Nervous ");
q0.setSelected_answer("");
questionlist.add(q0);
Questions q1= new Questions();
q1.setQuestion("Anixious,or on edge");
q1.setSelected_answer("");
questionlist.add(q1);
Questions q2= new Questions();
q2.setQuestion("Not being able to stop or control worrying");
q2.setSelected_answer("");
questionlist.add(q2);
Questions q3 = new Questions();
q3 .setQuestion("Worry to much about differnt things ?");
q3 .setSelected_answer("");
questionlist.add(q3 );
Questions q4= new Questions();
q4.setQuestion("Troble relaxing");
q4.setSelected_answer("");
questionlist.add(q4);
Questions q5= new Questions();
q5.setQuestion("Being so restless that it's hard to set still");
q5.setSelected_answer("");
questionlist.add(q5);
Questions q6= new Questions();
q6.setQuestion("Becoming easily annyed or irritable");
q6.setSelected_answer("");
questionlist.add(q6);
final TextView t= view.findViewById(R.id.question);
t.setText(questionlist.get(CURRENT_POSTION).getQuestion());
RelativeLayout rOptionA=(RelativeLayout) view.findViewById(R.id.rel_option_a);
RelativeLayout rOptionB=(RelativeLayout) view.findViewById(R.id.rel_option_b);
RelativeLayout rOptionC=(RelativeLayout) view.findViewById(R.id.rel_option_c);
RelativeLayout rOptionD=(RelativeLayout) view.findViewById(R.id.rel_option_d);
rOptionA.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// Interpret MotionEvent data
// Handle touch here
questionlist.get(CURRENT_POSTION).setSelected_answer("A");
iconA.setImageResource(R.drawable.ic_success);
iconB.setImageResource(R.drawable.ic_dot);
iconC.setImageResource(R.drawable.ic_dot);
iconD.setImageResource(R.drawable.ic_dot);
return false;
}
});
rOptionB.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// Interpret MotionEvent data
// Handle touch heresuper.onTouchEvent(event);
questionlist.get(CURRENT_POSTION).setSelected_answer("B");
iconA.setImageResource(R.drawable.ic_dot);
iconB.setImageResource(R.drawable.ic_success);
iconC.setImageResource(R.drawable.ic_dot);
iconD.setImageResource(R.drawable.ic_dot);
return true;
}
});
rOptionC.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// Interpret MotionEvent data
// Handle touch here
questionlist.get(CURRENT_POSTION).setSelected_answer("C");
iconA.setImageResource(R.drawable.ic_dot);
iconB.setImageResource(R.drawable.ic_dot);
iconC.setImageResource(R.drawable.ic_success);
iconD.setImageResource(R.drawable.ic_dot);
return true;
}
});
rOptionD.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
questionlist.get(CURRENT_POSTION).setSelected_answer("D");
iconA.setImageResource(R.drawable.ic_dot);
iconB.setImageResource(R.drawable.ic_dot);
iconC.setImageResource(R.drawable.ic_dot);
iconD.setImageResource(R.drawable.ic_success);
return true;
}
});
ConstraintLayout cl= view.findViewById(R.id.constraintLayout);
cl.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {
#Override
public void onSwipeDown() {
}
#Override
public void onSwipeLeft() {
if(CURRENT_POSTION < FINAL_POSITION) {
CURRENT_POSTION = CURRENT_POSTION + 1;
iconA.setImageResource(R.drawable.ic_dot);
iconB.setImageResource(R.drawable.ic_dot);
iconC.setImageResource(R.drawable.ic_dot);
iconD.setImageResource(R.drawable.ic_dot);
Log.i("ggg", String.valueOf(CURRENT_POSTION));
question = view.findViewById(R.id.question);
question.setText(questionlist.get(CURRENT_POSTION).getQuestion());
if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("")){
iconA.setImageResource(R.drawable.ic_dot);
iconB.setImageResource(R.drawable.ic_dot);
iconC.setImageResource(R.drawable.ic_dot);
iconD.setImageResource(R.drawable.ic_dot);
}
else{
Log.d("ELSE",( questionlist.get(CURRENT_POSTION).getSelected_answer()).toString());
if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("A")){
iconA.setImageResource(R.drawable.ic_success);
}
if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("B")){
iconB.setImageResource(R.drawable.ic_success);
} if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("C")){
iconC.setImageResource(R.drawable.ic_success);
}
if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("D")){
iconD.setImageResource(R.drawable.ic_success);
}
}
SlideAnimationUtil.slideInFromRight(getContext(),llQuestion);
} else{
}
}
#Override
public void onSwipeUp() {
}
#Override
public void onSwipeRight() {
if(CURRENT_POSTION >START_POSITION) {
CURRENT_POSTION = CURRENT_POSTION -1;
Log.i("ggg", String.valueOf(CURRENT_POSTION));
iconA.setImageResource(R.drawable.ic_dot);
iconB.setImageResource(R.drawable.ic_dot);
iconC.setImageResource(R.drawable.ic_dot);
iconD.setImageResource(R.drawable.ic_dot);
TextView t = view.findViewById(R.id.question);
t.setText(questionlist.get(CURRENT_POSTION).getQuestion());
if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("")){
iconA.setImageResource(R.drawable.ic_dot);
iconB.setImageResource(R.drawable.ic_dot);
iconC.setImageResource(R.drawable.ic_dot);
iconD.setImageResource(R.drawable.ic_dot);
}
else{
Log.d("ELSE",( questionlist.get(CURRENT_POSTION).getSelected_answer()).toString());
if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("A")){
iconA.setImageResource(R.drawable.ic_success);
}
if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("B")){
iconB.setImageResource(R.drawable.ic_success);
} if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("C")){
iconC.setImageResource(R.drawable.ic_success);
}
if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("D")){
iconD.setImageResource(R.drawable.ic_success);
}
}
SlideAnimationUtil.slideInFromLeft(getContext(),llQuestion);
}
}
});
}
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");
}
}
#Override
public void onDetach() {
super.onDetach();
CURRENT_POSTION=0;
mListener = null;
}
}
My xml code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
android:src="#drawable/three"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.0" />
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="0dp"
android:background="#d4ffffff"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="0dp"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<android.support.constraint.Guideline
android:id="#+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.25"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="152dp" />
<TextView
android:id="#+id/question"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:text="TextView"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="35sp"
app:layout_constraintBottom_toTopOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.25" />
<RelativeLayout
android:id="#+id/rel_option_a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/edge_rounded_border"
android:clickable="true"
android:padding="#dimen/option_relative_padding"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline4">
<ImageView
android:id="#+id/ic_option_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:tag="im_optA"
app:srcCompat="#drawable/ic_dot" />
<TextView
android:id="#+id/option_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toEndOf="#+id/ic_option_a"
android:text="#string/option_a"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="17sp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rel_option_b"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="14dp"
android:background="#drawable/edge_rounded_border"
android:padding="#dimen/option_relative_padding"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rel_option_a">
<TextView
android:id="#+id/option_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toEndOf="#+id/ic_option_b"
android:text="#string/option_b"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="17sp" />
<ImageView
android:id="#+id/ic_option_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:tag="im_optB"
app:srcCompat="#drawable/ic_dot" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rel_option_c"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="14dp"
android:background="#drawable/edge_rounded_border"
android:padding="#dimen/option_relative_padding"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rel_option_b">
<ImageView
android:id="#+id/ic_option_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:tag="im_optC"
app:srcCompat="#drawable/ic_dot" />
<TextView
android:id="#+id/option_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toEndOf="#+id/ic_option_c"
android:background="#drawable/edge_rounded"
android:text="#string/option_c"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="17sp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rel_option_d"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:background="#drawable/edge_rounded_border"
android:padding="#dimen/option_relative_padding"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rel_option_c">
<ImageView
android:id="#+id/ic_option_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:tag="im_optD"
app:srcCompat="#drawable/ic_dot" />
<TextView
android:id="#+id/option_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toEndOf="#+id/ic_option_d"
android:background="#drawable/edge_rounded"
android:text="#string/option_d"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="17sp" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
Above is my full code of fragment.I need to swipe without selecting the layer.how can I code.this image show the selection of particular layer,I am not able to swipe over this layer without selection.