Fixed scrolling to a post in PostAdapter.

captchafix
Florens Douwes 11 years ago
parent ebbfb3a33e
commit 3d7298ff05
  1. 9
      Chan/src/org/floens/chan/adapter/PostAdapter.java
  2. 3
      Chan/src/org/floens/chan/fragment/PostRepliesFragment.java
  3. 13
      Chan/src/org/floens/chan/manager/ThreadManager.java
  4. 13
      Chan/src/org/floens/chan/service/PinnedService.java
  5. 116
      Chan/src/org/floens/chan/utils/ScrollerRunnable.java
  6. 7
      Chan/src/org/floens/chan/view/PostView.java

@ -6,6 +6,7 @@ import java.util.List;
import org.floens.chan.R;
import org.floens.chan.manager.ThreadManager;
import org.floens.chan.model.Post;
import org.floens.chan.utils.ScrollerRunnable;
import org.floens.chan.utils.Utils;
import org.floens.chan.view.PostView;
import org.floens.chan.view.ThreadWatchCounterView;
@ -129,10 +130,14 @@ public class PostAdapter extends BaseAdapter {
}
public void scrollToPost(Post post) {
notifyDataSetChanged();
for (int i = 0; i < postList.size(); i++) {
if (postList.get(i).no == post.no) {
// listView.smoothScrollToPosition(i); does not work when a view is taller than the container
listView.setSelection(i);
// listView.smoothScrollToPosition(i);
ScrollerRunnable r = new ScrollerRunnable(listView);
r.start(i);
break;
}

@ -100,11 +100,12 @@ public class PostRepliesFragment extends DialogFragment {
final Post p = getItem(position);
postView.setPost(p, manager);
postView.setOnClickListener(new View.OnClickListener() {
postView.setOnClickListeners(new View.OnClickListener() {
@Override
public void onClick(View v) {
manager.closeAllPostFragments();
dismiss();
manager.highlightPost(p);
manager.scrollToPost(p);
}
});

@ -49,11 +49,12 @@ public class ThreadManager implements Loader.LoaderListener {
private final ThreadManager.ThreadManagerListener threadManagerListener;
private final List<List<Post>> popupQueue = new ArrayList<List<Post>>();
private PostRepliesFragment currentPopupFragment;
private Post highlightedPost;
private Loader loader;
public ThreadManager(Activity context, final ThreadManagerListener listener) {
this.activity = context;
activity = context;
threadManagerListener = listener;
}
@ -88,6 +89,8 @@ public class ThreadManager implements Loader.LoaderListener {
} else {
Logger.e(TAG, "Loader already unbinded");
}
highlightedPost = null;
}
public void requestData() {
@ -212,6 +215,14 @@ public class ThreadManager implements Loader.LoaderListener {
threadManagerListener.onScrollTo(post);
}
public void highlightPost(Post post) {
highlightedPost = post;
}
public boolean isPostHightlighted(Post post) {
return highlightedPost != null && post.board.equals(highlightedPost.board) && post.no == highlightedPost.no;
}
private void copyToClipboard(String comment) {
ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Post text", comment);

@ -19,24 +19,11 @@ public class PinnedService extends Service {
private static final long FOREGROUND_INTERVAL = 10000L;
private static final long BACKGROUND_INTERVAL = 60000L;
private static PinnedService instance;
private static boolean activityInForeground = false;
private Thread loadThread;
private boolean running = true;
public PinnedService() {
instance = this;
}
/**
* Get the PinnedService instance
* @return the instance or null
*/
public static PinnedService getInstance() {
return instance;
}
public static void onActivityStart() {
Logger.test("onActivityStart");
activityInForeground = true;

@ -0,0 +1,116 @@
package org.floens.chan.utils;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ListView;
public class ScrollerRunnable implements Runnable {
private static final int SCROLL_DURATION = 1;
private static final int MOVE_DOWN_POS = 1;
private static final int MOVE_UP_POS = 2;
private final ListView mList;
private int mMode;
private int mTargetPos;
private int mLastSeenPos;
private final int mExtraScroll;
public ScrollerRunnable(ListView listView) {
mList = listView;
mExtraScroll = ViewConfiguration.get(mList.getContext()).getScaledFadingEdgeLength();
}
public void start(int position) {
stop();
final int firstPos = mList.getFirstVisiblePosition();
final int lastPos = firstPos + mList.getChildCount() - 1;
int viewTravelCount = 0;
if (position <= firstPos) {
viewTravelCount = firstPos - position + 1;
mMode = MOVE_UP_POS;
} else if (position >= lastPos) {
viewTravelCount = position - lastPos + 1;
mMode = MOVE_DOWN_POS;
} else {
// Already on screen, nothing to do
return;
}
mTargetPos = position;
mLastSeenPos = ListView.INVALID_POSITION;
mList.post(this);
}
void stop() {
mList.removeCallbacks(this);
}
@Override
public void run() {
final int listHeight = mList.getHeight();
final int firstPos = mList.getFirstVisiblePosition();
switch (mMode) {
case MOVE_DOWN_POS: {
final int lastViewIndex = mList.getChildCount() - 1;
final int lastPos = firstPos + lastViewIndex;
if (lastViewIndex < 0) {
return;
}
if (lastPos == mLastSeenPos) {
// No new views, let things keep going.
// mList.post(this);
// return;
}
final View lastView = mList.getChildAt(lastViewIndex);
final int lastViewHeight = lastView.getHeight();
final int lastViewTop = lastView.getTop();
final int lastViewPixelsShowing = listHeight - lastViewTop;
final int extraScroll = lastPos < mList.getCount() - 1 ? mExtraScroll : mList.getPaddingBottom();
mList.smoothScrollBy(lastViewHeight - lastViewPixelsShowing + extraScroll, 0);
mLastSeenPos = lastPos;
if (lastPos < mTargetPos) {
mList.post(this);
}
break;
}
case MOVE_UP_POS: {
if (firstPos == mLastSeenPos) {
// No new views, let things keep going.
// mList.post(this);
// return;
}
final View firstView = mList.getChildAt(0);
if (firstView == null) {
return;
}
final int firstViewTop = firstView.getTop();
final int extraScroll = firstPos > 0 ? mExtraScroll : mList.getPaddingTop();
mList.smoothScrollBy(firstViewTop - extraScroll, 0);
mLastSeenPos = firstPos;
if (firstPos > mTargetPos) {
mList.post(this);
}
break;
}
default:
break;
}
}
}

@ -198,6 +198,8 @@ public class PostView extends LinearLayout implements View.OnClickListener, View
}
if (post.isSavedReply) {
full.setBackgroundColor(0xFFBCBCBC);
} else if (manager.isPostHightlighted(post)) {
full.setBackgroundColor(0xFFD6BAD0);
} else {
full.setBackgroundColor(0x00000000);
@ -297,6 +299,11 @@ public class PostView extends LinearLayout implements View.OnClickListener, View
full.setOnLongClickListener(this);
}
public void setOnClickListeners(View.OnClickListener listener) {
commentView.setOnClickListener(listener);
full.setOnClickListener(listener);
}
public void onLinkableClick(PostLinkable linkable) {
manager.onPostLinkableClicked(linkable);
}

Loading…
Cancel
Save