0

这是第一次在这里发帖。所以我有一个在 Android 上使用 Java 创建的编剧应用程序。它有一个简单的布局,如下所示。活动布局

因此,当用户单击“写入”时,他们将能够使用纯文本编写剧本,但要遵循称为 Fountain 的特定语法。下面给出一个例子。

如果用户编写以下文本...

作家(微笑)我必须这样做。

该应用程序会将文本拆分为行,并将不同的行添加到列表字符串(listy)中,然后从那里,当用户单击阅读选项卡时,应用程序将从 listy 的位置 0 开始并检查该项目是否为场景标题、动作或对话,它会根据它来格式化文本。动作和场景标题重力是“左”,而对话重力是“中心”。

一切正常,我没有收到任何错误。然而,问题只有一个。

当用户点击阅读时,应用程序会显示如下所示的脚本。

这是用户写的。

这是用户在格式化剧本后看到的。

问题是格式化后,列表视图不显示所有项目。就像我上面提到的,我将纯文本分成单独的行,并将它们添加到 ListString(listy),然后在确定 ListString(listy) 中的项目是否是动作、对话或场景标题之后,我创建带有键“type”和“content”的地图,type 是场景标题、对话或动作,content 是 ListString(listy) 中的项目。

然后我将地图添加到 ListMap(fountain) 并将其设置为 listview 数据。

我遇到的问题是列表视图没有显示所有项目。当我滚动时,有些项目变得可见,而有些则不可见。并且侧面的滚动条不动,它停留在顶部,就好像我根本没有滚动一样。

起初我以为是我的手机(Samsung J7 Neo),但我也在三星 S10+ 上测试了该应用程序,但问题仍然存在。

请帮帮我。可能是什么问题呢?

注意:我使用 Sketchware 来开发我的应用程序,而不是 Android Studio。

这是片段的 XML 代码。

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="3dp"
        android:choiceMode="none"
        android:layout_weight="1"
        android:divider="@android:color/transparent"/>
</LinearLayout>

这是 ReadScriptFragmentActivity.java

public class ReadscriptFragmentActivity extends  Fragment  { 
    
    
    private HashMap<String, Object> map = new HashMap<>();
    private String path = "";
    private String SCRIPT = "";
    private double pos = 0;
    private HashMap<String, Object> map2 = new HashMap<>();
    private String item1 = "";
    private boolean isUpperCaseOnly = false;
    private double pos1 = 0;
    private String character = "";
    private String item2 = "";
    private String typeBind = "";
    
    private ArrayList<String> listy = new ArrayList<>();
    private ArrayList<HashMap<String, Object>> fountain = new ArrayList<>();
    private ArrayList<String> listy2 = new ArrayList<>();
    
    private ListView lv;
    
    private SharedPreferences sp;
    private SharedPreferences script;
    private Notification not;
    @NonNull
    @Override
    public View onCreateView(@NonNull LayoutInflater _inflater, @Nullable ViewGroup _container, @Nullable Bundle _savedInstanceState) {
        View _view = _inflater.inflate(R.layout.readscript_fragment, _container, false);
        initialize(_savedInstanceState, _view);
        initializeLogic();
        return _view;
    }
    
    private void initialize(Bundle _savedInstanceState, View _view) {
        
        lv = (ListView) _view.findViewById(R.id.lv);
        sp = getContext().getSharedPreferences("sp", Activity.MODE_PRIVATE);
        script = getContext().getSharedPreferences("script", Activity.MODE_PRIVATE);
    }
    
    private void initializeLogic() {
        _getScript();
    }
    
    @Override
    public void onActivityResult(int _requestCode, int _resultCode, Intent _data) {
        
        super.onActivityResult(_requestCode, _resultCode, _data);
        
        switch (_requestCode) {
            
            default:
            break;
        }
    }
    
    @Override
    public void onResume() {
        super.onResume();
        _getScript();
    }
    public void _getScript () {
        path = script.getString("path", "");
        if (path.equals("")) {
            SketchwareUtil.showMessage(getContext(), "Error Opening Script!");
        }
        else {
            if (FileUtil.readFile(path).equals("")) {
                SketchwareUtil.showMessage(getContext(), "Error Opening Script!");
            }
            else {
                map = new Gson().fromJson(FileUtil.readFile(path), new TypeToken<HashMap<String, Object>>(){}.getType());
                SCRIPT = map.get("script").toString();
                listy = new ArrayList<String>(Arrays.asList(SCRIPT.split("\n\n")));
                lv.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, listy));
                pos = 0;
                for(int _repeat35 = 0; _repeat35 < (int)(listy.size()); _repeat35++) {
                    item1 = listy.get((int)(pos));
                    if (item1.contains("\n")) {
                        item2 = item1.substring((int)(0), (int)(item1.indexOf("\n") - 1));
                        if (item2.equals(item2.toUpperCase())) {
                            map2 = new HashMap<>();
                            map2.put("type", "dialogue");
                            map2.put("content", "          ".concat(item1));
                            fountain.add(map2);
                        }
                        else {
                            map2 = new HashMap<>();
                            map2.put("type", "action");
                            map2.put("content", item1);
                            fountain.add(map2);
                        }
                    }
                    else {
                        if (item1.equals(item1.toUpperCase())) {
                            if (item1.startsWith("INT.") || item1.startsWith("EXT.")) {
                                map2 = new HashMap<>();
                                map2.put("type", "scene");
                                map2.put("content", item1);
                                fountain.add(map2);
                            }
                            else {
                                map2 = new HashMap<>();
                                map2.put("type", "action");
                                map2.put("content", item1);
                                fountain.add(map2);
                            }
                        }
                        else {
                            map2 = new HashMap<>();
                            map2.put("type", "action");
                            map2.put("content", item1);
                            fountain.add(map2);
                        }
                    }
                    pos++;
                }
            }
        }
        lv.setAdapter(new LvAdapter(fountain));
    }
    
    
    public class LvAdapter extends BaseAdapter {
        ArrayList<HashMap<String, Object>> _data;
        public LvAdapter(ArrayList<HashMap<String, Object>> _arr) {
            _data = _arr;
        }
        
        @Override
        public int getCount() {
            return _data.size();
        }
        
        @Override
        public HashMap<String, Object> getItem(int _index) {
            return _data.get(_index);
        }
        
        @Override
        public long getItemId(int _index) {
            return _index;
        }
        @Override
        public View getView(final int _position, View _v, ViewGroup _container) {
            LayoutInflater _inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View _view = _v;
            if (_view == null) {
                _view = _inflater.inflate(R.layout.scriptreaditem, null);
            }
            
            final LinearLayout all = (LinearLayout) _view.findViewById(R.id.all);
            final TextView scene = (TextView) _view.findViewById(R.id.scene);
            final TextView action = (TextView) _view.findViewById(R.id.action);
            final TextView dialogue = (TextView) _view.findViewById(R.id.dialogue);
            
            typeBind = _data.get((int)_position).get("type").toString();
            if (typeBind.equals("scene")) {
                action.setVisibility(View.GONE);
                dialogue.setVisibility(View.GONE);
                scene.setText(_data.get((int)_position).get("content").toString());
                scene.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/courier_prime_regular.ttf"), 1);
            }
            if (typeBind.equals("action")) {
                scene.setVisibility(View.GONE);
                dialogue.setVisibility(View.GONE);
                action.setText(_data.get((int)_position).get("content").toString());
                action.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/courier_prime_regular.ttf"), 0);
            }
            if (typeBind.equals("dialogue")) {
                scene.setVisibility(View.GONE);
                action.setVisibility(View.GONE);
                dialogue.setText(_data.get((int)_position).get("content").toString());
                dialogue.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/courier_prime_regular.ttf"), 0);
            }
            
            return _view;
        }
    }
}

请帮忙。我不确定问题可能是什么。我不是开发人员。我通过 Sketchware 学习了 Java。所以请耐心等待我。先感谢您。

编辑:我发现了问题。如果您查看 ListView onBindCustomView,我添加了三个 if 语句来检查项目是对话、场景标题还是动作。问题是我为它们中的每一个添加了单独的 if 语句。所以我所做的就是将它们放在一个 if-else 语句中,例如如果项目是场景,则格式化为场景,否则如果项目是动作,则格式化为动作,否则如果项目是对话,则格式化为对话。所以简而言之,我将其他 if 语句放在前一个 if 语句的“else”部分下。(对不起,如果我的英语不好)。这是新的更新代码。公共类 ReadscriptFragmentActivity 扩展片段 {

private HashMap<String, Object> map = new HashMap<>();
private String path = "";
private String SCRIPT = "";
private double pos = 0;
private HashMap<String, Object> map2 = new HashMap<>();
private String item1 = "";
private boolean isUpperCaseOnly = false;
private double pos1 = 0;
private String character = "";
private String item2 = "";
private String typeBind = "";

private ArrayList<String> listy = new ArrayList<>();
private ArrayList<HashMap<String, Object>> fountain = new ArrayList<>();
private ArrayList<String> listy2 = new ArrayList<>();

private ListView lv;

private SharedPreferences sp;
private SharedPreferences script;
private Notification not;
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater _inflater, @Nullable ViewGroup _container, @Nullable Bundle _savedInstanceState) {
    View _view = _inflater.inflate(R.layout.readscript_fragment, _container, false);
    initialize(_savedInstanceState, _view);
    initializeLogic();
    return _view;
}

private void initialize(Bundle _savedInstanceState, View _view) {
    
    lv = (ListView) _view.findViewById(R.id.lv);
    sp = getContext().getSharedPreferences("sp", Activity.MODE_PRIVATE);
    script = getContext().getSharedPreferences("script", Activity.MODE_PRIVATE);
}

private void initializeLogic() {
    _getScript();
}

@Override
public void onActivityResult(int _requestCode, int _resultCode, Intent _data) {
    
    super.onActivityResult(_requestCode, _resultCode, _data);
    
    switch (_requestCode) {
        
        default:
        break;
    }
}

@Override
public void onResume() {
    super.onResume();
    _getScript();
}
public void _getScript () {
    path = script.getString("path", "");
    if (path.equals("")) {
        SketchwareUtil.showMessage(getContext(), "Error Opening Script!");
    }
    else {
        if (FileUtil.readFile(path).equals("")) {
            SketchwareUtil.showMessage(getContext(), "Error Opening Script!");
        }
        else {
            map = new Gson().fromJson(FileUtil.readFile(path), new TypeToken<HashMap<String, Object>>(){}.getType());
            SCRIPT = map.get("script").toString();
            listy = new ArrayList<String>(Arrays.asList(SCRIPT.split("\n\n")));
            pos = 0;
            for(int _repeat35 = 0; _repeat35 < (int)(listy.size()); _repeat35++) {
                item1 = listy.get((int)(pos));
                if (item1.contains("\n")) {
                    item2 = item1.substring((int)(0), (int)(item1.indexOf("\n") - 1));
                    if (item2.equals(item2.toUpperCase())) {
                        map2 = new HashMap<>();
                        map2.put("type", "dialogue");
                        map2.put("content", "          ".concat(item1));
                        fountain.add(map2);
                    }
                    else {
                        map2 = new HashMap<>();
                        map2.put("type", "action");
                        map2.put("content", item1);
                        fountain.add(map2);
                    }
                }
                else {
                    if (item1.equals(item1.toUpperCase())) {
                        if (item1.startsWith("INT.") || item1.startsWith("EXT.")) {
                            map2 = new HashMap<>();
                            map2.put("type", "scene");
                            map2.put("content", item1);
                            fountain.add(map2);
                        }
                        else {
                            map2 = new HashMap<>();
                            map2.put("type", "action");
                            map2.put("content", item1);
                            fountain.add(map2);
                        }
                    }
                    else {
                        map2 = new HashMap<>();
                        map2.put("type", "action");
                        map2.put("content", item1);
                        fountain.add(map2);
                    }
                }
                pos++;
            }
        }
    }
    lv.setAdapter(new LvAdapter(fountain));
}


public class LvAdapter extends BaseAdapter {
    ArrayList<HashMap<String, Object>> _data;
    public LvAdapter(ArrayList<HashMap<String, Object>> _arr) {
        _data = _arr;
    }
    
    @Override
    public int getCount() {
        return _data.size();
    }
    
    @Override
    public HashMap<String, Object> getItem(int _index) {
        return _data.get(_index);
    }
    
    @Override
    public long getItemId(int _index) {
        return _index;
    }
    @Override
    public View getView(final int _position, View _v, ViewGroup _container) {
        LayoutInflater _inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View _view = _v;
        if (_view == null) {
            _view = _inflater.inflate(R.layout.scriptreaditem, null);
        }
        
        final LinearLayout all = (LinearLayout) _view.findViewById(R.id.all);
        final TextView scene = (TextView) _view.findViewById(R.id.scene);
        final TextView action = (TextView) _view.findViewById(R.id.action);
        final TextView dialogue = (TextView) _view.findViewById(R.id.dialogue);
        
        scene.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/courier_prime_regular.ttf"), 1);
        action.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/courier_prime_regular.ttf"), 0);
        dialogue.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/courier_prime_regular.ttf"), 0);
        typeBind = _data.get((int)_position).get("type").toString();
        if (typeBind.equals("scene")) {
            action.setVisibility(View.GONE);
            dialogue.setVisibility(View.GONE);
            scene.setVisibility(View.VISIBLE);
            scene.setText(_data.get((int)_position).get("content").toString());
        }
        else {
            if (typeBind.equals("action")) {
                scene.setVisibility(View.GONE);
                dialogue.setVisibility(View.GONE);
                action.setVisibility(View.VISIBLE);
                action.setText(_data.get((int)_position).get("content").toString());
            }
            else {
                if (typeBind.equals("dialogue")) {
                    scene.setVisibility(View.GONE);
                    action.setVisibility(View.GONE);
                    dialogue.setVisibility(View.VISIBLE);
                    dialogue.setText(_data.get((int)_position).get("content").toString());
                }
            }
        }
        
        return _view;
    }
}

}

感谢那些回复的人。我想这个问题现在已经回答了。干杯。

4

0 回答 0