I have been working with Team Blox TreeView for about 3 month now with no issues, but after the update from 0.1.0 to (0.1.1 or 0.1.2), I have experienced the following strange situation. I have tried to reproduce the situation I have been experiencing using the code below:
1) TreeViewTest.class:--------------
public class TreeViewTest extends AppCompatActivity {
private static final String TAG = "TreeViewTest";
private DrawerLayout dl_treeview_out;
private ActionBarDrawerToggle drawerToggle;
private FrameLayout fl_treeview_out;
private TreeView treev_treeview_out;
private BaseTreeAdapter adapter_out;
private TreeView treev_treeview_in;
private BaseTreeAdapter adapter_in;
private TextView tv_treeview_in;
private Button b_treeview_in;
private View inView;
private TreeNode mCurrentNodeOut;
private TreeNode mCurrentNodeIn;
private List<String> items;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.treeview_out);
items = new ArrayList<String>();
for(int i = 0 ; i<=10 ; i++){
if(i == 0) {
items.add(i, "A" + "_" + "B");
}else{
items.add(i, "A" + i + "_" + "B" + i);
}
}
// Out View -------------------------------- START
dl_treeview_out = (DrawerLayout) findViewById(R.id.dl_treeview_out);
drawerToggle = new ActionBarDrawerToggle(this, dl_treeview_out , R.string.open, R.string.close) {
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
};
try {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}catch (Exception e){
Log.e(TAG , " " , e);
}
treev_treeview_out = (TreeView) dl_treeview_out.findViewById(R.id.treev_treeview_out);
treev_treeview_out.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mCurrentNodeOut = adapter_out.getNode(i);
if(mCurrentNodeOut.getData().toString().contains("A")){
fl_treeview_out.removeAllViews();
fl_treeview_out.addView(inView);
tv_treeview_in.setText("In View");
b_treeview_in.setText("~");
TreeNode inNode = new TreeNode("B");
adapter_in.setRootNode(inNode);
dl_treeview_out.openDrawer(Gravity.START , true);
}
}
});
adapter_out = new BaseTreeAdapter<ViewHolderOut>(TreeViewTest.this, R.layout.treeview_out_node) {
#NonNull
#Override
public ViewHolderOut onCreateViewHolder(View view) {
return new ViewHolderOut(view);
}
#Override
public void onBindViewHolder(ViewHolderOut viewHolder, Object data, int position) {
viewHolder.tv_out.setText(data.toString());
}
};
treev_treeview_out.setAdapter(adapter_out);
TreeNode outNode = new TreeNode("A");
adapter_out.setRootNode(outNode);
fl_treeview_out = (FrameLayout) dl_treeview_out.findViewById(R.id.fl_treeview_out);
// Out View -------------------------------- END
// In View -------------------------------- START
inView = getLayoutInflater().inflate(R.layout.treeview_in , null);
treev_treeview_in = (TreeView) inView.findViewById(R.id.treev_treeview_in);
treev_treeview_in.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mCurrentNodeIn = adapter_in.getNode(i);
if(mCurrentNodeIn.getData().toString().contains("B")){
Toast.makeText(getApplicationContext() , "Data In: " + mCurrentNodeIn.getData().toString() , Toast.LENGTH_LONG).show();
}
}
});
adapter_in = new BaseTreeAdapter<ViewHolderIn>(TreeViewTest.this, R.layout.treeview_in_node) {
#NonNull
#Override
public ViewHolderIn onCreateViewHolder(View view) {
return new ViewHolderIn(view);
}
#Override
public void onBindViewHolder(ViewHolderIn viewHolder, Object data, int position) {
viewHolder.tv_in.setText(data.toString());
}
};
treev_treeview_in.setAdapter(adapter_in);
tv_treeview_in = (TextView) inView.findViewById(R.id.tv_treeview_in);
b_treeview_in = (Button) inView.findViewById(R.id.b_treeview_in);
b_treeview_in.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
makeSelectListDialog("Select", TreeViewTest.this, items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
String[] data_split = items.get(i).split("_");
if(data_split.length == 2){
if(data_split[0].contains("A") && data_split[1].contains("B")){
mCurrentNodeOut.setData(data_split[0]);
adapter_in.getNode(0).setData(data_split[1]);
adapter_in.notifyDataChanged(adapter_in.getNode(0));
adapter_out.notifyDataChanged(mCurrentNodeOut);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).show();
}
});
// In View -------------------------------- END
}
private class ViewHolderOut {
CardView cv_out;
TextView tv_out;
ViewHolderOut(View view) {
cv_out = (CardView) view.findViewById(R.id.cv_out);
tv_out = (TextView) cv_out.findViewById(R.id.tv_out);
}
}
private class ViewHolderIn {
CardView cv_in;
TextView tv_in;
ViewHolderIn(View view) {
cv_in = (CardView) view.findViewById(R.id.cv_in);
tv_in = (TextView) cv_in.findViewById(R.id.tv_in);
}
}
public static <T> AlertDialog makeSelectListDialog(String prompt,
Context finalContext, List<T> listItems, final DialogInterface.OnClickListener onYesListener,
final DialogInterface.OnClickListener onNoListener) {
final ArrayAdapter<T> adapt =
new ArrayAdapter<T>(finalContext, android.R.layout.select_dialog_item, listItems);
AlertDialog a = new AlertDialog.Builder(finalContext)
.setTitle(prompt)
.setAdapter(adapt, onYesListener)
.setNegativeButton("Cancel", onNoListener)
.create();
return a;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}else {
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
}
2) treeview_out.xml:--------------------
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/dl_treeview_out"
android:layout_width="match_parent"
android:background="#android:color/white"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<de.blox.treeview.TreeView
android:id="#+id/treev_treeview_out"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:useMaxSize="true"
app:lineColor="#android:color/holo_blue_dark"/>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/fl_treeview_out">
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
3) treeview_out_node.xml:---------
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardElevation="18dp"
card_view:contentPadding="11dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="?attr/colorAccent"
android:textStyle="bold"/>
</LinearLayout>
</android.support.v7.widget.CardView>
4) treeview_in.xml:----------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="100"
android:id="#+id/ll_treeview_in"
android:layout_gravity="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="10"
android:background="#color/colorPrimaryDark"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textStyle="bold"
android:gravity="center"
android:layout_toStartOf="#id/b_treeview_in"
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:background="?attr/colorPrimary"
android:textColor="#android:color/white"
android:id="#+id/tv_treeview_in"/>
<Button
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentEnd="true"
android:textAllCaps="false"
android:layout_marginEnd="2.5dp"
android:layout_marginStart="2.5dp"
android:layout_marginTop="2.5dp"
android:layout_marginBottom="2.5dp"
android:textColor="#android:color/white"
android:background="#color/colorPrimary"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/b_treeview_in"/>
</RelativeLayout>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_weight="90"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#fff">
<de.blox.treeview.TreeView
android:id="#+id/treev_treeview_in"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:levelSeparation="20dp"
app:lineColor="#android:color/holo_green_light"/>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
5) treeview_in_node.xml:-------------------
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardElevation="18dp"
card_view:contentPadding="11dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="?attr/colorAccent"
android:textStyle="bold"/>
</LinearLayout>
</android.support.v7.widget.CardView>
6) In the code above, I have created two TreeViews (one directly inside activity and another one inside a drawer in this activity) with one block each. First TreeView block contains data starting with A (A , A1 , A2 , ...), where as Second TreeView block contains data starting with B (B , B1 , B2 , ...). The idea here is that I'm trying to change the data of first TreeView block and second TreeView block at the same time by clicking a button from inside the drawer (b_treeview_in) and selecting data from a list which opens. The above code works properly (data of both TreeView blocks is getting updated) in version 0.1.0 of this library, but not working properly (First TreeView block data is getting updated, but the second TreeView block data is not) after the update to version (0.1.1 or 0.1.2). Note: In both cases if I check the data, I find that it changed to what was selected from the list, but visually in case of the second TreeView block it didn't change to what was selected.
7) If this problem is caused by notifyDataChanged() or adapter.getNode(), then why is it working for the first TreeView block?
8)
In case of version 0.1.0
In case of version 0.1.1 or 0.1.2
Developer of TreeView here!
Unfortunatly this issues was causes by the library. I uploaded a new Version, which should fix it. Please try it out.
Related
I am trying to implement one ListView in activity. In my XML there is a ListView to fill that ListView data I have invoked one WebService in AsynkTask. I have used BaseAdapter to set that ListView. Now, Whenever I came on that activity the background becomes black for fraction seconds of time and than disappear. Same things happened when I longpress on ListView. The screen shot had attached with this question as well as following are the code that I have implemented.
Main Activity's XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:background="#color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.agraeta.user.btl.ComboOfferListActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#color/strip_bg"
android:textColor="#color/white"
android:gravity="center"
android:textSize="17sp"
android:text="COMBO OFFERS"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollingCache="false"
android:cacheColorHint="#color/white"
android:id="#+id/list_comboOffers"
android:background="#color/white"
android:divider="#android:color/transparent"
android:dividerHeight="5dp"/>
</LinearLayout>
Java File Of Main Activity :
public class ComboOfferListActivity extends AppCompatActivity implements Callback<ComboOfferData> {
ListView list_comboOffers;
ComboOfferListAdapter offerListAdapter;
List<ComboOfferItem> offerItemList=new ArrayList<>();
AdminAPI adminAPI;
Custom_ProgressDialog dialog;
String userID="";
String roleID="";
AppPrefs prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_combo_offer_list);
prefs=new AppPrefs(this);
adminAPI=ServiceGenerator.getAPIServiceClass();
dialog=new Custom_ProgressDialog(this,"Please Wait...");
dialog.setCancelable(false);
userID=prefs.getUserId();
roleID=prefs.getUserRoleId();
if(roleID.equals(C.ADMIN) || roleID.equals(C.COMP_SALES_PERSON) || roleID.equals(C.DISTRIBUTOR_SALES_PERSON)){
roleID=prefs.getSubSalesId();
userID=prefs.getSalesPersonId();
}
fetchIDs();
setActionBar();
}
private void fetchIDs() {
list_comboOffers=(ListView) findViewById(R.id.list_comboOffers);
offerListAdapter=new ComboOfferListAdapter(offerItemList,ComboOfferListActivity.this);
list_comboOffers.setAdapter(offerListAdapter);
list_comboOffers.setCacheColorHint(Color.TRANSPARENT);
list_comboOffers.requestFocus(0);
list_comboOffers.setScrollingCacheEnabled(false);
dialog.show();
Log.e("UR",userID+"-->"+roleID);
Call<ComboOfferData> offerDataCall=adminAPI.comboOfferDataCall(userID,roleID);
offerDataCall.enqueue(this);
}
private void setActionBar() {
// TODO Auto-generated method stub
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionBar.setCustomView(R.layout.actionbar_design);
View mCustomView = mActionBar.getCustomView();
ImageView image_drawer = (ImageView) mCustomView.findViewById(R.id.image_drawer);
ImageView img_home = (ImageView) mCustomView.findViewById(R.id.img_home);
ImageView img_notification = (ImageView) mCustomView.findViewById(R.id.img_notification);
FrameLayout unread = (FrameLayout) mCustomView.findViewById(R.id.unread);
image_drawer.setImageResource(R.drawable.ic_action_btl_back);
img_home.setVisibility(View.GONE);
img_notification.setVisibility(View.GONE);
unread.setVisibility(View.GONE);
image_drawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
#Override
public void onResponse(Call<ComboOfferData> call, Response<ComboOfferData> response) {
dialog.dismiss();
offerItemList.clear();
ComboOfferData offerData=response.body();
if(offerData.isStatus()){
offerItemList.addAll(offerData.getOfferItemList());
}
else {
Globals.Toast2(this,offerData.getMessage());
}
Log.e("Size","-->"+offerItemList.size());
offerListAdapter.notifyDataSetChanged();
}
#Override
public void onFailure(Call<ComboOfferData> call, Throwable t) {
dialog.dismiss();
Globals.showError(t,this);
}
#Override
public void onBackPressed() {
finish();
}
}
Adapter Class :
public class ComboOfferListAdapter extends BaseAdapter {
List<ComboOfferItem> offerItemList=new ArrayList<>();
Activity activity;
LayoutInflater inflater;
public ComboOfferListAdapter(List<ComboOfferItem> offerItemList, Activity activity) {
this.offerItemList = offerItemList;
this.activity = activity;
inflater=activity.getLayoutInflater();
}
#Override
public int getCount() {
return offerItemList.size();
}
#Override
public Object getItem(int position) {
return offerItemList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder=new ViewHolder();
convertView=inflater.inflate(R.layout.layout_combooffer_list,parent,false);
holder.img_offerThumb=(ImageView) convertView.findViewById(R.id.img_offerThumb);
holder.txt_offerTitle=(TextView) convertView.findViewById(R.id.txt_offerTitle);
holder.card_comboOffer = (CardView) convertView.findViewById(R.id.card_comboOffer);
holder.layout_combo = (LinearLayout) convertView.findViewById(R.id.layout_combo);
convertView.setTag(holder);
}
else {
holder= (ViewHolder) convertView.getTag();
}
Picasso.with(activity).load(Globals.IMAGE_LINK+offerItemList.get(position).getImagePath())
.into(holder.img_offerThumb);
holder.txt_offerTitle.setText(offerItemList.get(position).getOfferTitle());
holder.img_offerThumb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(activity, ComboOfferActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("combo_id", offerItemList.get(position).getOfferID());
activity.startActivity(intent);
}
});
holder.txt_offerTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(activity, ComboOfferActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("combo_id", offerItemList.get(position).getOfferID());
activity.startActivity(intent);
}
});
return convertView;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
Log.e("Called","-->"+offerItemList.size());
}
private class ViewHolder {
ImageView img_offerThumb;
TextView txt_offerTitle;
CardView card_comboOffer;
LinearLayout layout_combo;
}
}
Layout File For Adapter's View :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="match_parent"
android:background="#color/white"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/card_comboOffer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical"
cardBackgroundColor="#fff"
app:cardCornerRadius="2dp"
app:cardElevation="5dp"
app:theme="#style/CardView.Dark">
<LinearLayout
android:id="#+id/layout_combo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/img_offerThumb"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_gravity="center"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:scaleType="fitXY"/>
<TextView
android:id="#+id/txt_offerTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f58634"
android:padding="5dp"
android:textColor="#color/white"
android:layout_gravity="center"
android:textSize="15sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
I check everything in my code,I ask a question and do some of the modification,and also reading all the question about "Item not showing in RecyclerView",but the layout still not appear in my recyclerView.
This is my previous question,I attach all my code here,have 3 answer tell me,I should use LinearLayout instead of RelativeLayout as the parent of RecyclerView,so I modified the code as below
Fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/commentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/titlebar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/feed_item_margin"
android:layout_marginRight="#dimen/feed_item_margin"
android:layout_marginTop="#dimen/feed_item_margin"
android:layout_weight="1"
android:text="Be the first to like this" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/comment_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:animateLayoutChanges="false"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:id="#+id/commentInsert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="horizontal">
<EditText
android:id="#+id/commentField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:ems="10"
android:hint="Add a comment" />
<Button
android:id="#+id/sendButton"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
</LinearLayout>
Here is the item which should be appear in the RecyclerView.comment_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageView
android:id="#+id/commentProfilePic"
android:layout_weight="0.05"
android:layout_width="#dimen/comment_item_profile_pic"
android:layout_height="#dimen/comment_item_profile_pic"
android:layout_marginLeft="#dimen/feed_item_margin"
android:layout_marginRight="#dimen/feed_item_margin"
android:layout_marginTop="#dimen/feed_item_margin"
android:scaleType="fitCenter" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/feed_item_margin"
android:layout_marginRight="#dimen/feed_item_margin"
android:layout_marginTop="#dimen/feed_item_margin"
android:orientation="vertical"
android:layout_weight="1"
android:paddingLeft="#dimen/comment_item_profile_info_padd">
<TextView
android:id="#+id/commentUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/comment_item_status_pad_left_right"
android:paddingRight="#dimen/comment_item_status_pad_left_right"
android:textStyle="bold" />
<TextView
android:id="#+id/commentBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/comment_item_status_pad_left_right"
android:paddingRight="#dimen/comment_item_status_pad_left_right" />
<TextView
android:id="#+id/commentTimestamp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/comment_item_status_pad_left_right"
android:paddingRight="#dimen/comment_item_status_pad_left_right"
android:paddingTop="#dimen/comment_item_timestamp_pad_top" />
</LinearLayout>
I read this issue tell that need to add this line of code adapter.notifyDataSetChanged();,I done that,this is how I notifyDataSetChanged() when I parse the JSON.
private void parseJsonFeed(JSONObject response) {
try {
JSONArray commentArray = response.getJSONArray("comment");
//get all the item in Json
for (int i = 0; i < commentArray.length(); i++) {
JSONObject commentObj = (JSONObject) commentArray.get(i);
commentId = commentObj.getInt("comment_id");
commenterUsername= commentObj.getString("commenter_username");
commentBody = commentObj.getString("comment_body");
commenterProfileImage = commentObj.getString("commenter_profile_image");
commentCreatedAt = commentObj.getString("comment_created_at");
//set all item to the Array list
setItemToCommentArrayList(commentId,commenterUsername,commenterProfileImage,commentBody,commentCreatedAt);
}
// notify data changes to list adapter
commentAdapter.notifyDataSetChanged();
}catch (JSONException e){
System.out.println("end of content");
}
}
I checked,whether is something wrong when I set my adapter to the RecycleView.So i read this answer,and here is how I set my adapter
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View dialogView = inflater.inflate(R.layout.fragment_comment, container,false);
commentRecyclerView =(RecyclerView)dialogView.findViewById(R.id.comment_recycler_view);
commentRecyclerView.setNestedScrollingEnabled(false);
//bind the recycler view with the adapter
commentAdapter = new CommentAdapter(getActivity(),commentItems);
final LinearLayoutManager myLayoutManager = new LinearLayoutManager(getActivity());
commentRecyclerView.setLayoutManager(myLayoutManager);
commentRecyclerView.setAdapter(commentAdapter);
Here is my CommentAdpater , I still didnt see any different with my another recycleview adapter.
public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.MyViewHolder>{
private Context cContext;
private List<CommentItem> commentItems;
class MyViewHolder extends RecyclerView.ViewHolder{
TextView commentBody,commentUsername,commentTimeStamp;
ImageView commentProfilePic;
//find all the view here
MyViewHolder(final View view) {
super(view);
commentProfilePic = (ImageView)view.findViewById(R.id.commentProfilePic);
commentUsername = (TextView)view.findViewById(R.id.commentUsername);
commentBody = (TextView)view.findViewById(R.id.commentBody);
commentTimeStamp = (TextView)view.findViewById(R.id.commentTimestamp);
}
}
public CommentAdapter(Context cContext, List<CommentItem> commentItems) {
this.cContext = cContext;
this.commentItems = commentItems;
}
#Override
public long getItemId(int position) {
return position;
}
//this one for make the adview inside this
#Override
public int getItemViewType(int position) {
return position;
}
//bind the comment item here
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View commentItemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.comment_item, parent, false);
return new MyViewHolder(commentItemView);
}
//do all the action here
#Override
public void onBindViewHolder(CommentAdapter.MyViewHolder holder, int position) {
final CommentItem commentItem = commentItems.get(position);
//commenter username
holder.commentUsername.setText(commentItem.getUsername());
//commenter profile image
Glide
.with(cContext)
.load(commentItem.getCommentProfilePic())
.fitCenter()
.into(holder.commentProfilePic);
//comment body
holder.commentBody.setText(commentItem.getCommentBody());
//comment timestamp
holder.commentTimeStamp.setText(commentItem.getCommentTimeStamp());
}
#Override
public int getItemCount() {
return commentItems.size();
}
}
I make sure my JSON is received in my volley Request.I log it out,is received in Volley onResponse .My JSON is look like this
"comment":[{"comment_created_at":"2017-03-21 13:03:40","comment_id":8,"comment_body":"abc","commenter_username":"abc","profile_image_path":"http:\/\/abc\/def\/v1\/ef\/defaultmale.jpg"
And this question I ask just tell me no need to worry about cause I using Glide to load my image,Glide will handle that
This is all the solution that tried,and still havent get it done,so what I still missing out here,in order the comment_xmlappear to the recyclerView inside Fragment.xml? Any guide that lead me to the right direction is well-appreciated.Thanks
UPDATE
Here is my setItemToCommentArrayList()
private void setItemToCommentArrayList(int commentId, String commenterUsername, String commenterProfileImage, String commentBody, String commentCreatedAt) {
CommentItem item = new CommentItem();
item.setCommentId(commentId);
item.setUsername(commenterUsername);
item.setCommentProfilePic(commenterProfileImage);
item.setCommentBody(commentBody);
item.setCommentTimeStamp(commentCreatedAt);
//save it to the comment array list
commentItems.add(item);
}
Here is my data model of Comment Item
public class CommentItem {
private String username,commentBody,commentTimeStamp,commentProfilePic;
private int commentId;
public CommentItem(){
}
public CommentItem(int commentId, String username,String commentBody,String commentTimeStamp,String commentProfilePic){
super();
this.commentId = commentId;
this.username = username;
this.commentBody = commentBody;
this.commentProfilePic= commentTimeStamp;
this.commentTimeStamp= commentProfilePic;
}
public int getCommentId() {
return commentId;
}
public void setCommentId(int commentId) {
this.commentId = commentId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getCommentBody() {
return commentBody;
}
public void setCommentBody(String commentBody) {
this.commentBody = commentBody;
}
public String getCommentTimeStamp() {
return commentTimeStamp;
}
public void setCommentTimeStamp(String commentTimeStamp) {
this.commentTimeStamp = commentTimeStamp;
}
public String getCommentProfilePic() {
return commentProfilePic;
}
public void setCommentProfilePic(String commentProfilePic) {
this.commentProfilePic = commentProfilePic;
}
}
First of all try to print out your dataSet size into getItemCount method of recyclerview -
#Override
public int getItemCount() {
Log.e("Size of data:", "" +commentItems.size())
return commentItems.size();
}
If it returns greater than zero - there may be a problem with your row_item of recyclerview. Double check your comment_item.xml file. Possibly try to remove weights.
Are items not showing in the cards or is the recycler view itself not showing?
If the recycler view itself is not showing, maybe the problem is inside the getItemCount the size being sent is 0.
do a check there and return 1 if size is zero.
#Override
public int getItemCount() {
if(commentItems.size() == 0)
return 1;
else
return commentItems.size();
}
My Problem is the last item in the listview is not displaying even though it has been added to the adapter and as well as in the list as i can see the number of items in the adapter is ok. Is it the problem with the layout? this is my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/golive_bg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/Room"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus android:layout_width="wrap_content" />
</EditText>
<Button
android:id="#+id/Search1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Search" />
<ImageButton
android:id="#+id/CreateRoomimageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/closed_doorplus" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/listRooms"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fastScrollEnabled="true"
android:transcriptMode="alwaysScroll">
</ListView>
</LinearLayout>
</LinearLayout>
This is my class activity
public class chatRoomsListActivity extends Activity implements OnScrollListener {
private static String Password;
private ListView lv;
private static Collection<HostedRoom> rooms;
private static ArrayList<String> Roomlist = new ArrayList<String>();
private static Handler mHandler = new Handler();
private Button SearchButton;
private ImageButton CreateButton;
private EditText EditTextSearch;
private String Search;
private String RoomName;
private static ChatRoomListAdapter adapter;
private static String Nickname="";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_rooms);
SearchButton=(Button)findViewById(R.id.Search1);
CreateButton=(ImageButton)findViewById(R.id.CreateRoomimageButton);
lv = (ListView) findViewById(R.id.listRooms);
adapter = new ChatRoomListAdapter(getApplicationContext(),
R.layout.chat_rooms_wrapper);
lv.setAdapter(adapter);
//ScrollView.measureChildWithMargins();
try {
populate_RoomList();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
addListenerOnButton();
/*
arrayAdapter = new ArrayAdapter<String>(
this,android.R.layout.simple_list_item_1,Roomlist);
lv.setAdapter(arrayAdapter);
*/
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parnet,
android.view.View view,
int position, long id) {
setRoomName(ChatRoomListAdapter.getItemName(position));
showOptionDialog();
}
});
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
CreateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
createRoomDialog();
}
});
SearchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
EditTextSearch=(EditText) findViewById(R.id.Room);
Search=EditTextSearch.getText().toString();
//Search=Search.concat("*");
try {
// populate_RoomList();
getSearchlist();
// updateList();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void getSearchlist() throws XMPPException{
Roomlist.clear();
adapter.clear();
ChatRoomListAdapter.getChatRoom().clear();
rooms=getHostedRooms(MyService.getConnection(),"conference."+
MyService.getDomain());
System.out.println("in getsearch");
for (HostedRoom h : rooms)
{
System.out.println(h.getName().toString()+" contain " +Search);
if(h.getName().toString().contains(Search)){
System.out.println("roomlist= " +h.getJid().toString());
Roomlist.add(h.getName());
System.out.println("ChatRooms.IsRoomPassProtected(h.getName())==
"+ChatRooms.IsRoomPassProtected(h.getName()));
if(ChatRooms.IsRoomPassProtected(h.getName())==true){
adapter.add(new ChatRoom(h.getName(),true));
}
else if(ChatRooms.IsRoomPassProtected(h.getName())==false){
adapter.add(new ChatRoom(h.getName(),false));
}
}
}
System.out.println("Adapter count="+adapter.getCount());
}
private void populate_RoomList() throws XMPPException {
//chatRoomsList.getRooms();"
Roomlist.clear();
adapter.clear();
//rooms.clear();
ChatRoomListAdapter.getChatRoom().clear();
rooms=getHostedRooms(MyService.getConnection(),"conference."+
MyService.getDomain());
//System.out.println(rooms.iterator().next().getName());
for (HostedRoom h : rooms)
{
System.out.println("roomlist= " +h.getName().toString());
Roomlist.add(h.getName());
System.out.println("ChatRooms.IsRoomPassProtected(h.getName())==
"+ChatRooms.IsRoomPassProtected(h.getName()));
if(ChatRooms.IsRoomPassProtected(h.getName())==true){
adapter.add(new ChatRoom(h.getName(),true));
}
else if(ChatRooms.IsRoomPassProtected(h.getName())==false){
adapter.add(new ChatRoom(h.getName(),false));
}
}
System.out.println("Adapter count="+adapter.getCount());
}
public void setRoomName(String roomName) {
RoomName = roomName;
}
public static Collection<HostedRoom> getHostedRooms(Connection connection, String serviceName)
throws XMPPException {
List<HostedRoom> answer = new ArrayList<HostedRoom>();
ServiceDiscoveryManager discoManager = new
ServiceDiscoveryManager(connection);
DiscoverItems items = discoManager.discoverItems(serviceName);
for (Iterator<DiscoverItems.Item> it = items.getItems(); it.hasNext();) {
answer.add(new HostedRoom(it.next()));
}
return answer;
}
}
And this is my custom adapter
public class ChatRoomListAdapter extends ArrayAdapter<ChatRoom> {
private TextView name;
private static List<ChatRoom> ChatRoom = new ArrayList<ChatRoom>();
private LinearLayout wrapper;
private ImageView lock;
public ChatRoomListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public static List<ChatRoom> getChatRoom() {
return ChatRoom;
}
public static void setChatRoom(List<ChatRoom> chatRoom) {
ChatRoom = chatRoom;
}
public ChatRoom getItem(int index) {
return ChatRoomListAdapter.ChatRoom.get(index);
}
public static String getItemName(int index) {
return ChatRoomListAdapter.ChatRoom.get(index).name;
}
public static void setfriends(List<Friends> friends) {
FriendListAdapter.setFriends(friends);
}
public void add(ChatRoom object) {
System.out.println("IN Chat room list adapter "+" addded");
ChatRoomListAdapter.ChatRoom.add(object);
super.add(object);
}
public void remove(ChatRoom object) {
ChatRoom.remove(object);
super.remove(object);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater)
this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.chat_rooms_wrapper, parent, false);
}
wrapper = (LinearLayout) row.findViewById(R.id.C_R_wrapper);
name = (TextView) row.findViewById(R.id.C_R_RoomName);
lock=(ImageView)row.findViewById(R.id.C_R_Lock);
try {
Log.d("Chat room adapter", "heere");
ChatRoom CR= getItem(position);
System.out.println("name= "+CR.name);
name.setText(CR.name);
if(CR.lock==true)
{
lock.setImageResource(R.drawable.lock_lock_icon);
}else{
lock.setImageResource(R.drawable.lock_unlock_icon);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return row;
}
public Bitmap decodeToBitmap(byte[] decodedByte) {
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
}
Thanks in advance.
Add this line to your Viewpager
android:layout_marginBottom="?attr/actionBarSize"
because the size you don't have at the bottom is exactly the size taken by your Toolbar
Example with RecyclerView
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_trips"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_marginBottom="?attr/actionBarSize"/>
Example with Viewpager
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_trip_history"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="?attr/actionBarSize" />
Try this you can solve your problem with this:
You just need to change your xml file as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/golive_bg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="6" >
<EditText
android:id="#+id/Room"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_weight="4">
<requestFocus android:layout_width="wrap_content" />
</EditText>
<Button
android:id="#+id/Search1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Search"
android:layout_weight="1" />
<ImageButton
android:id="#+id/CreateRoomimageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/closed_doorplus"
android:layout_weight="1" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/listRooms"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fastScrollEnabled="true"
android:transcriptMode="alwaysScroll">
</ListView>
</RelativeLayout>
</LinearLayout>
You can get the last item inside getView of the Adapter and change it layout params height by setLayoutParams. But the minus is you have to correct other items height because the height will change when scroll the list up/down
Maybe its very simple. Try to set the layout_height of your listview in the xml file to wrap_content.
android:layout_height="wrap_content"
Try to set the second LinearLayout holding the Listview to match_parent height... It seems like your LinearLayout is growing with the ListView (wrap_content) out of your screen space
I have a class that extends a Fragment. I want when press a button from An Activity to opens this Fragment with its layout in dialog in addition to that the fragment opens normally in its place. Is this possible?
This is my Fragment (TipsFragment.java)
public class TipsFragment extends Fragment{
ListView list;
TipsAdapter tipsAdapter;
AlertDialog PageDialog;
DAO db;
Cursor c;
ArrayList<HashMap<String, String>> tipsList;
HashMap<String, String> map;
ArrayList<HashMap<String, String>> pages;
Integer tipType;
ImageButton page;
ImageButton add;
ImageButton search;
EditText inputAdd;
EditText inputSearch;
ImageView addImage;
RelativeLayout layoutAdd;
RelativeLayout layoutSearch;
int itemSelected;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
db = new DAO(activity);
db.open();
}
public TipsFragment(){}
public TipsFragment(int tipType, int itemSelected ){
this.tipType = tipType;
this.itemSelected = itemSelected;
}
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tips_fragement, container, false);
System.out.println("tipType: "+tipType);
System.out.println("itemSelected: "+itemSelected);
page = (ImageButton) rootView.findViewById(R.id.pager);
add = (ImageButton) rootView.findViewById(R.id.add);
search = (ImageButton) rootView.findViewById(R.id.search);
inputSearch = (EditText) rootView.findViewById(R.id.inputSearch);
inputAdd = (EditText) rootView.findViewById(R.id.inputAdd);
addImage = (ImageView) rootView.findViewById(R.id.imageAdd);
layoutAdd = (RelativeLayout) rootView.findViewById(R.id.layoutAdd);
layoutSearch = (RelativeLayout) rootView.findViewById(R.id.layoutSearch);
if (tipType != 0) {
switch (tipType) {
case 1:
System.out.println("All tips");
if (getActivity().getIntent().getStringExtra("startFrom") == null) {
c = db.getTips("0");
} else {
c = db.getTips(getActivity().getIntent().getStringExtra("startFrom"));
}
break;
case 2:
System.out.println("favorite tips");
c = db.getFavoriteTips();
page.setVisibility(View.GONE);
System.out.println("in favorite, count_records: "+c.getCount());
break;
}
}
System.out.println("count_records: "+c.getCount());
if (c.getCount() != 0) {
if (getActivity().getIntent().getStringExtra("startLabel") != null) {
page = (ImageButton) rootView.findViewById(R.id.pager);
}
tipsList = new ArrayList<HashMap<String, String>>();
list = (ListView) rootView.findViewById(R.id.tipList);
do {
map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(DAO.TIP_ID, c.getString(c.getColumnIndex(c.getColumnName(0))));
map.put(DAO.TIP_CONTENT, c.getString(c.getColumnIndex(c.getColumnName(1))));
// adding HashList to ArrayList
tipsList.add(map);
} while (c.moveToNext());
// Getting adapter by passing xml data ArrayList
tipsAdapter = new TipsAdapter(getActivity(), tipsList);
list.setAdapter(tipsAdapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
map = tipsList.get(position);
System.out.println("in list select item, tipType: "+tipType);
if (!MainActivity.tSlidingLayer.isOpened()) {
MainActivity.tSlidingLayer.openLayer(true);
}
}
});
page.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Strings to Show In Dialog with Radio Buttons
}
});
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
inputSearch.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
}
addImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "addImage pressed", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
and this is layout tips_fragement.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/titleBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="#drawable/add" />
<ImageButton
android:id="#+id/search"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="#drawable/search" />
<ImageButton
android:id="#+id/pager"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="#drawable/pager" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="5dip"
android:background="#drawable/shadow" >
</View>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" >
<EditText
android:id="#+id/inputAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Add" />
<ImageView
android:id="#+id/imageAdd"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/inputAdd"
android:layout_alignBottom="#+id/inputAdd"
android:layout_alignRight="#+id/inputAdd"
android:src="#android:drawable/ic_menu_add" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutSearch"
android:visibility="gone" >
<EditText
android:id="#+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search"/>
<ImageView
android:id="#+id/imageSearch"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/inputSearch"
android:layout_alignBottom="#+id/inputSearch"
android:layout_alignRight="#+id/inputSearch"
android:src="#android:drawable/ic_menu_search" />
</RelativeLayout>
<ListView
android:id="#+id/tipList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/buttons"
android:divider="#351802"
android:dividerHeight="2dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
Since Android supports nesting Fragments (available in the support library) you could show your TipsFragment as a content of another DialogFragment. Avoid this solution whenever possible.
A better solution would be to make TipsFragment extend DialogFragment and that way you can either show the Fragment inline or in a dialog.
1 - Make TipsFragment extends DialogFragment
public class TipsFragment extends DialogFragment {...}
2 - On button click use FragmentTransaction.add or FragmentTransaction.replace to show the DialogFragment inline or include it in your layout and change its visibility.
3 - Show the TipsFragment as a dialog
TipsFragment fr = new TipsFragment();
fr.show(getSupportFragmentManager(), "dialog_fragment_tag");
Guide: Performing Fragment Transactions. More samples are available in the ApiDemos application from the SDK samples
Here i have one view pager activity which has one imageview and 2 overlay bars. there overlay bars i made using android xml file layout itself.
Here my requirement is like that
1) single tap on view pager's imageview first time = show top and bottom rectangle overlaybar.
2) single tap on view pager's imageview second time = hide these overlays.
These both are functions like android gallary view type.
But here when these top and bottom layout bar displays at that time i want to use only buttons click only which buttons are declare within this layout.
But I am not getting success to achieve this thing.
Problems
1) when top or bottom bar is there if i can click on next or previous button than its takes event for behind imageview single tap touch event, And my bar is getting invisible.
2) Only wants declare buttons event only
3) Avoid imageview getting clicked when i touch to overlay bar.
In short when my top and bottom image bar appears at that time no touh event takes place for imageview from top and bottom image bar. I can click on imageview but not making clickable when i click on actually next or previous or share button.
So these are the problems which i am facing, Please help me .
Source code :
activity_pager_image.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<RelativeLayout
android:id="#+id/rl_top_overlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/slideshow_bar"
android:visibility="gone" >
<TextView
android:id="#+id/tv_top_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textIsSelectable="false" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl_bottom_overlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#drawable/slideshow_bar"
android:visibility="visible" >
<Button
android:id="#+id/btn_left_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="35dp"
android:background="#drawable/ic_left_arrow" />
<Button
android:id="#+id/btn_below_share"
style="#style/normalText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="35dp"
android:background="#drawable/ic_share"
android:visibility="visible" />
<Button
android:id="#+id/btn_right_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
android:layout_toRightOf="#id/btn_left_arrow"
android:background="#drawable/ic_right_arrow" />
</RelativeLayout>
</FrameLayout>
item_pager_image.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<demo.android.library.imagezoom.ImageViewTouch
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:contentDescription="#string/descr_image"
android:scaleType="fitXY" />
</FrameLayout>
JAVA code
public class ImagePagerActivity extends BaseActivity {
private static final String STATE_POSITION = "STATE_POSITION";
private DisplayImageOptions options;
private String[] imageUrls;
private ViewPager pager;
private static int sCounter = 0;
private RelativeLayout mRlTopOverlayBar = null;
private RelativeLayout mRlBottomOverlayBar = null;
private TextView mPageNumberText = null;
private Button mLeftArrow = null;
private Button mRightArrow = null;
int mPageCounter = 0;
int mTotalImages = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_pager);
mRlTopOverlayBar = (RelativeLayout) findViewById(R.id.rl_top_overlay);
mRlBottomOverlayBar = (RelativeLayout) findViewById(R.id.rl_bottom_overlay);
mPageNumberText = (TextView) findViewById(R.id.tv_top_overlay);
mLeftArrow = (Button) findViewById(R.id.btn_left_arrow);
mRightArrow = (Button) findViewById(R.id.btn_right_arrow);
Bundle bundle = getIntent().getExtras();
String[] imageUrls = bundle
.getStringArray(Constants.GALLARY_IMAGES_IMAGE_BUNDLE_KEY);
mTotalImages = imageUrls.length;
mPageCounter = bundle.getInt(
Constants.GALLARY_IMAGE_POSITION_BUNDLE_KEY, 0);
Log.d("TAG", "Pre Poistion " + mPageCounter);
if (savedInstanceState != null) {
mPageCounter = savedInstanceState.getInt(STATE_POSITION);
}
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.photo_default)
.showImageOnFail(R.drawable.ic_error).resetViewBeforeLoading()
.cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new FadeInBitmapDisplayer(300)).build();
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new ImagePagerAdapter(imageUrls));
pager.setCurrentItem(mPageCounter);
mLeftArrow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// int setCounter = mPageCounter - 1;
// if (setCounter >= 0) {
// }
pager.setCurrentItem(pager.getCurrentItem() - 1);
}
});
mRightArrow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
pager.setCurrentItem(pager.getCurrentItem() + 1);
/*
* int setCounter = mPageCounter + 1; if (setCounter <
* mTotalImages) { pager.setCurrentItem(mPageCounter + 1); }
*/
}
});
}
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_POSITION, pager.getCurrentItem());
}
private class ImagePagerAdapter extends PagerAdapter {
private String[] images;
private LayoutInflater inflater;
ImagePagerAdapter(String[] images) {
this.images = images;
inflater = getLayoutInflater();
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
#Override
public void finishUpdate(View container) {
}
#Override
public int getCount() {
return images.length;
}
#Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.item_pager_image,
view, false);
Log.d("TAG", "Poistion " + position);
final ImageViewTouch imageView = (ImageViewTouch) imageLayout
.findViewById(R.id.image);
final DeactivableViewPager viewPager = new DeactivableViewPager(
ImagePagerActivity.this);
imageView.setOnScaleListener(new OnPageScaleListener() {
#Override
public void onScaleBegin() {
viewPager.deactivate();
}
#Override
public void onScaleEnd(float scale) {
if (scale > 1.0) {
viewPager.deactivate();
} else {
viewPager.activate();
}
}
});
imageView
.setSingleTapListener(new OnImageViewTouchSingleTapListener() {
#Override
public void onSingleTapConfirmed() {
Log.d("TAG", "setSingleTapListener");
sCounter++;
if (sCounter % 2 == 0) {
mRlTopOverlayBar.setVisibility(View.GONE);
mRlBottomOverlayBar.setVisibility(View.GONE);
} else {
mRlTopOverlayBar.setVisibility(View.VISIBLE);
mRlBottomOverlayBar.setVisibility(View.VISIBLE);
mRlBottomOverlayBar.setClickable(false);
mRlTopOverlayBar.setClickable(false);
}
}
});
imageLoader.displayImage(images[position], imageView, options,
new SimpleImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
// spinner.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
String message = null;
switch (failReason.getType()) {
case IO_ERROR:
message = "Input/Output error";
break;
case DECODING_ERROR:
message = "Image can't be decoded";
break;
case NETWORK_DENIED:
message = "Downloads are denied";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
Toast.makeText(ImagePagerActivity.this, message,
Toast.LENGTH_SHORT).show();
// spinner.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri,
View view, Bitmap loadedImage) {
// spinner.setVisibility(View.GONE);
}
});
((ViewPager) view).addView(imageLayout, 0);
return imageLayout;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
#Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void startUpdate(View container) {
}
}
}
image :
thanks
A better way is to set the top and bottom frame to be clickable, with:
android:clickable="true"
Doing so will make sure that the view/frame itself will trap all clicking events, and will not pass it through the view behind it. Note this method works for all layout/view/controls, but many controls (such as buttons) already have this function on by default.
android:clickable="true" for top and bottom bar.
or give each FrameLayout an onClickListener.
just add this to each of your framelayout view containers so that absorb the click:
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
By default, some controls have clickable property as true.
But the layout doesn't. For layout, we have to make them explicitly clickable so that it will grab the events of clicks or touches and will not let it pass to background views. This is how we do it:
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
Add these lines to all your layouts, so that it will grab/absorb events and will not let it pass to background views.
#gordon1hd1 answer is correct but for those who are still confused, I am adding my layout which contains a FrameLayout as parent and a LinearLayout and twoImageViews as childs.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/scroll_parent"
android:orientation="horizontal" />
<ImageView
android:id="#+id/ivArrowLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:src="#drawable/actionbar_back"
android:layout_gravity="left|center_vertical"
android:background="#3f808080"
android:clickable="true"
/>
<ImageView
android:id="#+id/ivArrowRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:src="#drawable/actionbar_back"
android:layout_gravity="right|center_vertical"
android:background="#3f808080"
android:rotation="180"
android:clickable="true"
/>
</FrameLayout>
Previously, the Linearlayout was also intercepting touch events when either of ImageViews were pressed. Adding android:clickable="true" to both ImageViews resolved the issue.
If you are also facing this type of issue, add android:clickable="true" to the view you want to trap the clicking event.
Simply, Set android:clickable="true" in xml to your foreground view.
imageView.setSingleTapListener(new OnImageViewTouchSingleTapListener() {
#Override
public void onSingleTapConfirmed() {
Log.d("TAG", "setSingleTapListener");
sCounter++;
if (sCounter % 2 == 0) {
mRlTopOverlayBar.setVisibility(View.GONE);
mRlBottomOverlayBar.setVisibility(View.GONE);
pager.requestFocus();
} else {
mRlTopOverlayBar.setVisibility(View.VISIBLE);
mRlBottomOverlayBar.setVisibility(View.VISIBLE);
mRlTopOverlayBar.requestFocus();
mRlBottomOverlayBar.requestFocus();
mRlBottomOverlayBar.setClickable(true);
mRlTopOverlayBar.setClickable(true);
}
}
});
Add android:clickable="true" to rl_bottom_overlay and rl_top_overlay. If you donĀ“t set click events to these layouts (nor via XML neither programatically), no events will be triggered on background views.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/llSettings"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#ff106279"
android:minHeight="25px"
android:minWidth="25px"
android:onClick="click"
android:orientation="vertical"
android:visibility="visible">
<LinearLayout
android:id="#+id/llSettings1"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#ff000f"
android:clickable="true"
android:minHeight="25px"
android:minWidth="25px"
android:orientation="vertical"
android:visibility="visible">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="buttonclick"
android:text="New Button" />
</LinearLayout>
</RelativeLayout>
and
public void click(View v) {
Toast.makeText(this, "((RelativeLayout)v).toString()", Toast.LENGTH_SHORT).show();
}
public void buttonclick(View v) {
Toast.makeText(this, "Button", Toast.LENGTH_SHORT).show();
}
// For removing click event, sometime click=true not work
relBottomConfirm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
return;
}
});